home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / sysdep.c < prev    next >
C/C++ Source or Header  |  1993-11-20  |  121KB  |  4,872 lines

  1. /* Interfaces to system-dependent kernel and library entries.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <signal.h>
  21. #include <setjmp.h>
  22.  
  23. #include "config.h"
  24. #include "lisp.h"
  25. #include "blockinput.h"
  26. #undef NULL
  27.  
  28. #ifdef STDC_HEADERS
  29. #include <stdlib.h>
  30. #include <io.h>
  31. #endif
  32.  
  33. #ifndef min
  34. #define min(x,y) ((x) > (y) ? (y) : (x))
  35. #endif
  36.  
  37. /* In this file, open, read and write refer to the system calls,
  38.    not our sugared interfaces  sys_open, sys_read and sys_write.
  39.    Contrariwise, for systems where we use the system calls directly,
  40.    define sys_read, etc. here as aliases for them.  */
  41. #ifndef read
  42. #define sys_read read
  43. #define sys_write write
  44. #endif /* `read' is not a macro */
  45.  
  46. #ifdef WINDOWSNT
  47. #include <windows.h>
  48. #include "ntsig.h"
  49. #else /* !WINDOWSNT */
  50. #undef read
  51. #undef write
  52. #endif /* !WINDOWSNT */
  53.  
  54. #ifndef close
  55. #define sys_close close
  56. #else 
  57. #undef close
  58. #endif
  59.  
  60. #ifndef open
  61. #define sys_open open
  62. #else /* `open' is a macro */
  63. #undef open
  64. #endif /* `open' is a macro */
  65.  
  66. /* Does anyone other than VMS need this? */
  67. #ifndef fwrite
  68. #define sys_fwrite fwrite
  69. #else
  70. #undef fwrite
  71. #endif
  72.  
  73. #include <stdio.h>
  74. #include <sys/types.h>
  75. #include <sys/stat.h>
  76. #include <errno.h>
  77.  
  78. #ifndef WINDOWSNT
  79. extern int errno;
  80. #ifndef VMS
  81. extern char *sys_errlist[];
  82. #endif
  83. #endif
  84.  
  85. #ifdef VMS
  86. #include <rms.h>
  87. #include <ttdef.h>
  88. #include <tt2def.h>
  89. #include <iodef.h>
  90. #include <ssdef.h>
  91. #include <descrip.h>
  92. #include <fibdef.h>
  93. #include <atrdef.h>
  94. #include <ctype.h>
  95. #include <string.h>
  96. #ifdef __GNUC__
  97. #include <sys/file.h>
  98. #else
  99. #include <file.h>
  100. #endif
  101. #undef F_SETFL
  102. #ifndef RAB$C_BID
  103. #include <rab.h>
  104. #endif
  105. #define    MAXIOSIZE ( 32 * PAGESIZE )    /* Don't I/O more than 32 blocks at a time */
  106. #endif /* VMS */
  107.  
  108. #ifndef BSD4_1
  109. #ifdef BSD /* this is done this way to avoid defined (BSD) || defined (USG)
  110.           because the vms compiler doesn't grok `defined' */
  111. #include <fcntl.h>
  112. #endif
  113. #ifdef USG
  114. #ifndef USG5
  115. #include <fcntl.h>
  116. #endif
  117. #endif
  118. #endif /* not 4.1 bsd */
  119.  
  120. #ifdef BROKEN_FASYNC
  121. /* On some systems (DGUX comes to mind real fast) FASYNC causes
  122.    background writes to the terminal to stop all processes in the
  123.    process group when invoked under the csh (and probably any shell
  124.    with job control). This stops Emacs dead in its tracks when coming
  125.    up under X11. */
  126. #undef FASYNC
  127. #endif
  128.  
  129. #ifndef WINDOWSNT
  130. #ifdef mips
  131. #include <sys/ioctl.h>
  132. #endif
  133. #endif /* WINDOWSNT */
  134. #include "systty.h"
  135.  
  136. #ifdef BSD
  137. #ifdef BSD4_1
  138. #include <wait.h>
  139. #else /* not 4.1 */
  140. #include <sys/wait.h>
  141. #endif /* not 4.1 */
  142. #endif /* BSD */
  143.  
  144. #ifdef BROKEN_TIOCGWINSZ
  145. #undef TIOCGWINSZ
  146. #endif
  147.  
  148. #ifdef USG
  149. #include <sys/utsname.h>
  150. #include <string.h>
  151. #ifndef MEMORY_IN_STRING_H
  152. #include <memory.h>
  153. #endif
  154. #ifdef TIOCGWINSZ
  155. #ifdef NEED_SIOCTL
  156. #include <sys/sioctl.h>
  157. #endif
  158. #ifdef NEED_PTEM_H
  159. #include <sys/stream.h>
  160. #include <sys/ptem.h>
  161. #endif
  162. #endif /* TIOCGWINSZ */
  163. #endif /* USG */
  164.  
  165. extern int quit_char;
  166.  
  167. #include "frame.h"
  168. #include "window.h"
  169. #include "termhooks.h"
  170. #include "termchar.h"
  171. #include "termopts.h"
  172. #include "dispextern.h"
  173. #include "process.h"
  174.  
  175. #ifdef NONSYSTEM_DIR_LIBRARY
  176. #include "ndir.h"
  177. #endif /* NONSYSTEM_DIR_LIBRARY */
  178.  
  179. #include "syssignal.h"
  180. #include "systime.h"
  181.  
  182. #include "sysdep_p.h"
  183. #include "alloca_p.h"
  184. #include "term_p.h"
  185. #ifdef WINDOWSNT
  186. #include "ntterm_p.h"
  187. #include "ntproc_p.h"
  188. #include <direct.h>
  189. /* In process.h which conflicts with the local copy */
  190. #define _P_WAIT 0
  191. int _CRTAPI1 _spawnlp(int, const char *, const char *, ...);
  192. int _CRTAPI1 _getpid(void);
  193. #endif
  194. static SIGTYPE _CALLBACK_ select_alarm _P_((int sig));
  195.  
  196. static int baud_convert[] =
  197. #ifdef BAUD_CONVERT
  198.   BAUD_CONVERT;
  199. #else
  200.   {
  201.     0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
  202.     1800, 2400, 4800, 9600, 19200, 38400
  203.   };
  204. #endif
  205.  
  206. extern short ospeed;
  207.  
  208. /* The file descriptor for Emacs's input terminal.
  209.    Under Unix, this is always left zero;
  210.    under VMS, we place the input channel number here.
  211.    This allows us to write more code that works for both VMS and Unix.  */
  212. static int input_fd;
  213.  
  214. _VOID_
  215. discard_tty_input ()
  216. {
  217. #ifndef WINDOWSNT
  218.   struct emacs_tty buf;
  219.  
  220.   if (noninteractive)
  221.     return;
  222.  
  223.   /* Discarding input is not safe when the input could contain
  224.      replies from the X server.  So don't do it.  */
  225.   if (read_socket_hook)
  226.     return;
  227.  
  228. #ifdef VMS
  229.   end_kbd_input ();
  230.   SYS$QIOW (0, input_fd, IO$_READVBLK|IO$M_PURGE, input_iosb, 0, 0,
  231.         &buf.main, 0, 0, terminator_mask, 0, 0);
  232.   queue_kbd_input ();
  233. #else /* not VMS */
  234. #ifdef APOLLO
  235.   {
  236.     int zero = 0;
  237.     ioctl (0, TIOCFLUSH, &zero);
  238.   }
  239. #else /* not Apollo */
  240.   EMACS_GET_TTY (input_fd, &buf);
  241.   EMACS_SET_TTY (input_fd, &buf, 0);
  242. #endif /* not Apollo */
  243. #endif /* not VMS */
  244. #endif /* !WINDOWSNT */
  245. }
  246.  
  247. #ifdef SIGTSTP
  248.  
  249. _VOID_
  250. stuff_char (c)
  251.      char c;
  252. {
  253. /* Should perhaps error if in batch mode */
  254. #ifdef TIOCSTI
  255.   ioctl (0, TIOCSTI, &c);
  256. #else /* no TIOCSTI */
  257.   error ("Cannot stuff terminal input characters in this version of Unix.");
  258. #endif /* no TIOCSTI */
  259. }
  260.  
  261. #endif /* SIGTSTP */
  262.  
  263. _VOID_
  264. init_baud_rate ()
  265. {
  266. #ifdef WINDOWSNT
  267.   /*
  268.    * if we do things right in term.c, this is never called.
  269.    */
  270.   DebPrint(("EMACS broken @ "__FILE__ ": %d\n", __LINE__));
  271. #else /* !WINDOWSNT */
  272.  
  273.   if (noninteractive)
  274.     ospeed = 0;
  275.   else
  276.     {
  277. #ifdef VMS
  278.       struct sensemode sg;
  279.  
  280.       SYS$QIOW (0, input_fd, IO$_SENSEMODE, &sg, 0, 0,
  281.         &sg.class, 12, 0, 0, 0, 0 );
  282.       ospeed = sg.xmit_baud;
  283. #else /* not VMS */
  284. #ifdef HAVE_TERMIOS
  285.       struct termios sg;
  286.  
  287.       sg.c_cflag = (sg.c_cflag & ~CBAUD) | B9600;
  288.       tcgetattr (0, &sg);
  289.       ospeed = cfgetospeed (&sg);
  290. #else /* neither VMS nor TERMIOS */
  291. #ifdef HAVE_TERMIO
  292.       struct termio sg;
  293.  
  294.       sg.c_cflag = (sg.c_cflag & ~CBAUD) | B9600;
  295. #ifdef HAVE_TCATTR
  296.       tcgetattr (0, &sg);
  297. #else
  298.       ioctl (input_fd, TCGETA, &sg);
  299. #endif
  300.       ospeed = sg.c_cflag & CBAUD;
  301. #else /* neither VMS nor TERMIOS nor TERMIO */
  302.       struct sgttyb sg;
  303.       
  304.       sg.sg_ospeed = B9600;
  305.       if (ioctl (0, TIOCGETP, &sg) < 0)
  306.     abort ();
  307.       ospeed = sg.sg_ospeed;
  308. #endif /* not HAVE_TERMIO */
  309. #endif /* not HAVE_TERMIOS */
  310. #endif /* not VMS */
  311.     }
  312.    
  313.   baud_rate = (ospeed < sizeof baud_convert / sizeof baud_convert[0]
  314.             ? baud_convert[ospeed] : 9600);
  315.   if (baud_rate == 0)
  316.     baud_rate = 1200;
  317. #endif /* !WINDOWSNT */
  318. }
  319.  
  320. /*ARGSUSED*/
  321. _VOID_
  322. set_exclusive_use (fd)
  323.      int fd;
  324. {
  325. #ifdef FIOCLEX
  326.   ioctl (fd, FIOCLEX, 0);
  327. #endif
  328.   /* Ok to do nothing if this feature does not exist */
  329. }
  330.  
  331. #ifndef subprocesses
  332.  
  333. _VOID_
  334. wait_without_blocking ()
  335. {
  336. #ifdef BSD
  337.   wait3 (0, WNOHANG | WUNTRACED, 0);
  338. #elif WINDOWSNT
  339.   /* It would be trivial to make a version of Wait which didn't block
  340.      by playing with the timeout in the WaitForMultipleObjects
  341.      However, subprocesses is defined for WINDOWSNT so there's not much
  342.      point in fixing this function */
  343.   DbgPrint(("EMACS broken @ "__FILE__ ": %d\n", __LINE__));
  344. #else
  345.   croak ("wait_without_blocking");
  346. #endif
  347.   synch_process_alive = 0;
  348. }
  349.  
  350. #endif /* not subprocesses */
  351.  
  352. int wait_debugging;   /* Set nonzero to make following function work under dbx
  353.                  (at least for bsd).  */
  354.  
  355. SIGTYPE
  356. wait_for_termination_signal ()
  357. {}
  358.  
  359. /* Wait for subprocess with process id `pid' to terminate and
  360.    make sure it will get eliminated (not remain forever as a zombie) */
  361.  
  362. _VOID_
  363. wait_for_termination (pid)
  364.      int pid;
  365. {
  366.   while (1)
  367.     {
  368. #ifdef subprocesses
  369. #ifdef VMS
  370.       int status;
  371.  
  372.       status = SYS$FORCEX (&pid, 0, 0);
  373.       break;
  374. #else /* not VMS */
  375. #if defined (BSD) || (defined (HPUX) && !defined (HPUX_5))
  376.       /* Note that kill returns -1 even if the process is just a zombie now.
  377.      But inevitably a SIGCHLD interrupt should be generated
  378.      and child_sig will do wait3 and make the process go away. */
  379.       /* There is some indication that there is a bug involved with
  380.      termination of subprocesses, perhaps involving a kernel bug too,
  381.      but no idea what it is.  Just as a hunch we signal SIGCHLD to see
  382.      if that causes the problem to go away or get worse.  */
  383.       sigsetmask (sigmask (SIGCHLD));
  384.       if (0 > kill (pid, 0))
  385.         {
  386.       sigsetmask (SIGEMPTYMASK);
  387.       kill (getpid (), SIGCHLD);
  388.       break;
  389.     }
  390.       if (wait_debugging)
  391.     sleep (1);
  392.       else
  393.     sigpause (SIGEMPTYMASK);
  394. #else /* not BSD, and not HPUX version >= 6 */
  395. #if defined (UNIPLUS)
  396.       if (0 > kill (pid, 0))
  397.     break;
  398.       wait (0);
  399. #else /* neither BSD nor UNIPLUS: random sysV */
  400. #ifdef POSIX_SIGNALS    /* would this work for LINUX as well? */
  401.       sigblock (sigmask (SIGCHLD));
  402.       if (0 > kill (pid, 0))
  403.     {
  404.       sigunblock (sigmask (SIGCHLD));
  405.       break;
  406.     }
  407.       sigpause (SIGEMPTYMASK);
  408. #else /* not POSIX_SIGNALS */
  409. #ifdef HAVE_SYSV_SIGPAUSE
  410.       sighold (SIGCHLD);
  411.       if (0 > kill (pid, 0))
  412.     {
  413.       sigrelse (SIGCHLD);
  414.       break;
  415.     }
  416.       sigpause (SIGCHLD);
  417. #else /* not HAVE_SYSV_SIGPAUSE */
  418. #ifdef WINDOWSNT
  419.       if (wait(0) < 0) {
  420.       DebPrint(("wait_for_termination: waiting failed\n"));
  421.       }
  422.       break;
  423. #else /* !WINDOWSNT */
  424.       if (0 > kill (pid, 0))
  425.     break;
  426.       /* Using sleep instead of pause avoids timing error.
  427.      If the inferior dies just before the sleep,
  428.      we lose just one second.  */
  429.       sleep (1);
  430. #endif /* !WINDOWSNT */
  431. #endif /* not HAVE_SYSV_SIGPAUSE */
  432. #endif /* not POSIX_SIGNALS */
  433. #endif /* not UNIPLUS */
  434. #endif /* not BSD, and not HPUX version >= 6 */
  435. #endif /* not VMS */
  436. #else /* not subprocesses */
  437. #ifndef BSD4_1
  438.       if (kill (pid, 0) < 0)
  439.     break;
  440.       wait (0);
  441. #else /* BSD4_1 */
  442.       int status;
  443.       status = wait (0);
  444.       if (status == pid || status == -1)
  445.     break;
  446. #endif /* BSD4_1 */
  447. #endif /* not subprocesses */
  448.     }
  449. }
  450.  
  451. #ifdef subprocesses
  452.  
  453. /*
  454.  *    flush any pending output
  455.  *      (may flush input as well; it does not matter the way we use it)
  456.  */
  457.  
  458. _VOID_
  459. flush_pending_output (channel)
  460.      int channel;
  461. {
  462. #ifdef HAVE_TERMIOS
  463.   /* If we try this, we get hit with SIGTTIN, because
  464.      the child's tty belongs to the child's pgrp. */
  465. #else
  466. #ifdef TCFLSH
  467.   ioctl (channel, TCFLSH, 1);
  468. #else
  469. #ifdef TIOCFLUSH
  470.   int zero = 0;
  471.   /* 3rd arg should be ignored
  472.      but some 4.2 kernels actually want the address of an int
  473.      and nonzero means something different.  */
  474.   ioctl (channel, TIOCFLUSH, &zero);
  475. #endif
  476. #endif
  477. #endif
  478. }
  479.  
  480. #ifndef VMS
  481. /*  Set up the terminal at the other end of a pseudo-terminal that
  482.     we will be controlling an inferior through.
  483.     It should not echo or do line-editing, since that is done
  484.     in Emacs.  No padding needed for insertion into an Emacs buffer.  */
  485.  
  486. _VOID_
  487. child_setup_tty (out)
  488.      int out;
  489. {
  490. #ifdef WINDOWSNT
  491.     /*
  492.      * Shouldn't this stuff be in an #ifdef HAVE_PTYS?
  493.      */
  494. #else /* !WINDOWSNT */
  495.   struct emacs_tty s;
  496.  
  497.   EMACS_GET_TTY (out, &s);
  498.  
  499. #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
  500.   s.main.c_oflag |= OPOST;    /* Enable output postprocessing */
  501.   s.main.c_oflag &= ~ONLCR;    /* Disable map of NL to CR-NL on output */
  502.   s.main.c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
  503.                   /* No output delays */
  504.   s.main.c_lflag &= ~ECHO;    /* Disable echo */
  505.   s.main.c_lflag |= ISIG;    /* Enable signals */
  506.   s.main.c_iflag &= ~IUCLC;    /* Disable map of upper case to lower on
  507.                    input */
  508.   s.main.c_oflag &= ~OLCUC;    /* Disable map of lower case to upper on
  509.                    output */
  510. #if 0
  511.   /* Said to be unnecessary:  */
  512.   s.main.c_cc[VMIN] = 1;    /* minimum number of characters to accept  */
  513.   s.main.c_cc[VTIME] = 0;    /* wait forever for at least 1 character  */
  514. #endif
  515.  
  516.   s.main.c_lflag |= ICANON;    /* Enable erase/kill and eof processing */
  517.   s.main.c_cc[VEOF] = 04;    /* insure that EOF is Control-D */
  518.   s.main.c_cc[VERASE] = 0377;    /* disable erase processing */
  519.   s.main.c_cc[VKILL] = 0377;    /* disable kill processing */
  520.  
  521. #ifdef HPUX
  522.   s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
  523. #endif /* HPUX */
  524.  
  525. #ifdef AIX
  526. /* AIX enhanced edit loses NULs, so disable it */
  527. #ifndef IBMR2AIX
  528.   s.main.c_line = 0;
  529.   s.main.c_iflag &= ~ASCEDIT;
  530. #endif
  531.   /* Also, PTY overloads NUL and BREAK.
  532.      don't ignore break, but don't signal either, so it looks like NUL.  */
  533.   s.main.c_iflag &= ~IGNBRK;
  534.   s.main.c_iflag &= ~BRKINT;
  535.   /* QUIT and INTR work better as signals, so disable character forms */
  536.   s.main.c_cc[VINTR] = 0377;
  537. #ifdef SIGNALS_VIA_CHARACTERS
  538.   /* the QUIT and INTR character are used in process_send_signal
  539.      so set them here to something useful.  */
  540.   if (s.main.c_cc[VQUIT] == 0377)
  541.     s.main.c_cc[VQUIT] = '\\'&037;    /* Control-\ */
  542.   if (s.main.c_cc[VINTR] == 0377)
  543.     s.main.c_cc[VINTR] = 'C'&037;    /* Control-C */
  544. #else /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
  545.   /* QUIT and INTR work better as signals, so disable character forms */
  546.   s.main.c_cc[VQUIT] = 0377;
  547.   s.main.c_cc[VINTR] = 0377;
  548.   s.main.c_lflag &= ~ISIG;
  549. #endif /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
  550.   s.main.c_cc[VEOL] = 0377;
  551.   s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
  552. #endif /* AIX */
  553.  
  554. #else /* not HAVE_TERMIO */
  555.  
  556.   s.main.sg_flags &= ~(ECHO | CRMOD | ANYP | ALLDELAY | RAW | LCASE
  557.                | CBREAK | TANDEM);
  558.   s.main.sg_erase = 0377;
  559.   s.main.sg_kill = 0377;
  560.  
  561. #endif /* not HAVE_TERMIO */
  562.  
  563.   EMACS_SET_TTY (out, &s, 0);
  564.  
  565. #ifdef BSD4_1
  566.   if (interrupt_input)
  567.     reset_sigio ();
  568. #endif /* BSD4_1 */
  569. #ifdef RTU
  570.   {
  571.     int zero = 0;
  572.     ioctl (out, FIOASYNC, &zero);
  573.   }
  574. #endif /* RTU */
  575. #endif /* !WINDOWSNT */
  576. }
  577. #endif /* not VMS */
  578.  
  579. #endif /* subprocesses */
  580.  
  581. /*ARGSUSED*/
  582. _VOID_
  583. setpgrp_of_tty (pid)
  584.      int pid;
  585. {
  586.   EMACS_SET_TTY_PGRP (input_fd, &pid);
  587. }
  588.  
  589. /* Record a signal code and the handler for it.  */
  590. struct save_signal
  591. {
  592.   int code;
  593.   SIGTYPE (_CALLBACK_ *handler) _P_((int));
  594. };
  595.  
  596. /* Suspend the Emacs process; give terminal to its superior.  */
  597.  
  598. _VOID_
  599. sys_suspend ()
  600. {
  601. #ifdef VMS
  602.   /* "Foster" parentage allows emacs to return to a subprocess that attached
  603.      to the current emacs as a cheaper than starting a whole new process.  This
  604.      is set up by KEPTEDITOR.COM.  */
  605.   unsigned long parent_id, foster_parent_id;
  606.   char *fpid_string;
  607.  
  608.   fpid_string = getenv ("EMACS_PARENT_PID");
  609.   if (fpid_string != NULL)
  610.     {
  611.       sscanf (fpid_string, "%x", &foster_parent_id);
  612.       if (foster_parent_id != 0)
  613.     parent_id = foster_parent_id;
  614.       else
  615.     parent_id = getppid ();
  616.     }
  617.   else
  618.     parent_id = getppid ();
  619.  
  620.   xfree (fpid_string);        /* On VMS, this was malloc'd */
  621.  
  622.   if (parent_id && parent_id != 0xffffffff)
  623.     {
  624.       SIGTYPE (*oldsig)() = (int) signal (SIGINT, SIG_IGN);
  625.       int status = LIB$ATTACH (&parent_id) & 1;
  626.       signal (SIGINT, oldsig);
  627.       return status;
  628.     }
  629.   else
  630.     {
  631.       struct {
  632.     int    l;
  633.     char    *a;
  634.       } d_prompt;
  635.       d_prompt.l = sizeof ("Emacs: ");        /* Our special prompt */
  636.       d_prompt.a = "Emacs: ";            /* Just a reminder */
  637.       LIB$SPAWN (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, &d_prompt, 0);
  638.       return 1;
  639.     }
  640.   return -1;
  641. #else
  642. #ifdef SIGTSTP
  643.  
  644.   {
  645. #ifdef USG
  646.     int pgrp = getpgrp ();
  647. #else
  648.     int pgrp = getpgrp (0);
  649. #endif
  650.     EMACS_KILLPG (pgrp, SIGTSTP);
  651.   }
  652.  
  653. #else /* No SIGTSTP */
  654. #ifdef USG_JOBCTRL /* If you don't know what this is don't mess with it */
  655.   ptrace (0, 0, 0, 0);        /* set for ptrace - caught by csh */
  656.   kill (getpid (), SIGQUIT);
  657.  
  658. #else /* No SIGTSTP or USG_JOBCTRL */
  659.  
  660. /* On a system where suspending is not implemented,
  661.    instead fork a subshell and let it talk directly to the terminal
  662.    while we wait.  */
  663. #ifdef    WINDOWSNT
  664.  int pid = -1;
  665. #else /* !WINDOWSNT */
  666.   int pid = fork ();
  667. #endif /* !WINDOWSNT */
  668.   struct save_signal saved_handlers[5];
  669.  
  670. #ifdef    WINDOWSNT
  671.   saved_handlers[0].code = 0;
  672. #else    /* !WINDOWSNT */
  673.   saved_handlers[0].code = SIGINT;
  674.   saved_handlers[1].code = SIGQUIT;
  675.   saved_handlers[2].code = SIGTERM;
  676. #ifdef SIGIO
  677.   saved_handlers[3].code = SIGIO;
  678.   saved_handlers[4].code = 0;
  679. #else
  680.   saved_handlers[3].code = 0;
  681. #endif
  682. #endif /* !WINDOWSNT */
  683.  
  684. #ifndef    WINDOWSNT
  685.   if (pid == -1)
  686.     error ("Can't spawn subshell");
  687.   if (pid == 0)
  688. #endif    /* !WINDOWSNT */
  689.     {
  690.       char *sh;
  691.  
  692. #ifdef    WINDOWSNT
  693. extern    Lisp_Object    Vshell_file_name;
  694.       /*
  695.        * Is there a reason egetenv is used to get shell here when we have it
  696.        * in our own var?
  697.        */
  698.  
  699.       sh = XSTRING(Vshell_file_name)->data;
  700. #else 
  701.       sh = (char *) egetenv ("SHELL");
  702.       if (sh == 0)
  703.     sh = "sh";
  704. #endif    /* !WINDOWSNT */
  705.       /* Use our buffer's default directory for the subshell.  */
  706.       {
  707.     Lisp_Object dir;
  708.     unsigned char *str;
  709.     int len;
  710.  
  711.     /* mentioning current_buffer->buffer would mean including buffer.h,
  712.        which somehow wedges the hp compiler.  So instead... */
  713.  
  714.     dir = intern ("default-directory");
  715.     /* Can't use NULL */
  716.     if (XFASTINT (Fboundp (dir)) == XFASTINT (Qnil))
  717.       goto xyzzy;
  718.     dir = Fsymbol_value (dir);
  719.     if (XTYPE (dir) != Lisp_String)
  720.       goto xyzzy;
  721.  
  722.     str = (unsigned char *) alloca (XSTRING (dir)->size + 2);
  723.     len = XSTRING (dir)->size;
  724.     bcopy (XSTRING (dir)->data, str, len);
  725.     if (str[len - 1] != PATH_SEPARATOR) str[len++] = PATH_SEPARATOR;
  726.     str[len] = 0;
  727.     chdir (str);
  728.       }
  729.     xyzzy:
  730. #ifndef WINDOWSNT
  731. #ifdef subprocesses
  732.       close_process_descs ();    /* Close Emacs's pipes/ptys */
  733. #endif
  734. #endif
  735.  
  736. #ifdef PRIO_PROCESS
  737.       {
  738.     extern int emacs_priority;
  739.  
  740.     if (emacs_priority)
  741.       nice (-emacs_priority);
  742.       }
  743. #endif
  744.  
  745. #ifdef    WINDOWSNT
  746.       RestoreConsole();
  747.  
  748.       /* Waits for process completion */
  749.       pid = _spawnlp(_P_WAIT, sh, sh, NULL);
  750.       if (pid == -1)
  751.         write (1, "Can't execute subshell", 22);
  752.  
  753.       TakeConsole();
  754. #else    /* !WINDOWSNT */
  755.       execlp (sh, sh, 0);
  756.       write (1, "Can't execute subshell", 22);
  757.       _exit (1);
  758. #endif    /* !WINDOWSNT */
  759.     }
  760.  
  761. #ifndef    WINDOWSNT
  762.   save_signal_handlers (saved_handlers);
  763.   synch_process_alive = 1;
  764.   wait_for_termination (pid);
  765.   restore_signal_handlers (saved_handlers);
  766. #endif    /* !WINDOWSNT */
  767.  
  768. #endif /* no USG_JOBCTRL */
  769. #endif /* no SIGTSTP */
  770. #endif /* not VMS */
  771. }
  772.  
  773. _VOID_
  774. save_signal_handlers (saved_handlers)
  775.      struct save_signal *saved_handlers;
  776. {
  777.   while (saved_handlers->code)
  778.     {
  779.       saved_handlers->handler = signal (saved_handlers->code, SIG_IGN);
  780.       saved_handlers++;
  781.     }
  782. }
  783.  
  784. _VOID_
  785. restore_signal_handlers (saved_handlers)
  786.      struct save_signal *saved_handlers;
  787. {
  788.   while (saved_handlers->code)
  789.     {
  790.       signal (saved_handlers->code, saved_handlers->handler);
  791.       saved_handlers++;
  792.     }
  793. }
  794.  
  795. #ifdef F_SETFL
  796.  
  797. int old_fcntl_flags;
  798.  
  799. _VOID_
  800. init_sigio ()
  801. {
  802. #ifdef FASYNC
  803.   old_fcntl_flags = fcntl (0, F_GETFL, 0) & ~FASYNC;
  804. #endif
  805.   request_sigio ();
  806. }
  807.  
  808. _VOID_
  809. reset_sigio ()
  810. {
  811.   unrequest_sigio ();
  812. }
  813.  
  814. #ifdef FASYNC        /* F_SETFL does not imply existence of FASYNC */
  815.  
  816. _VOID_
  817. request_sigio ()
  818. {
  819. #ifdef SIGWINCH
  820.   sigunblock (sigmask (SIGWINCH));
  821. #endif
  822.   fcntl (0, F_SETFL, old_fcntl_flags | FASYNC);
  823.  
  824.   interrupts_deferred = 0;
  825. }
  826.  
  827. _VOID_
  828. unrequest_sigio ()
  829. {
  830. #ifdef SIGWINCH
  831.   sigblock (sigmask (SIGWINCH));
  832. #endif
  833.   fcntl (0, F_SETFL, old_fcntl_flags);
  834.   interrupts_deferred = 1;
  835. }
  836.  
  837. #else /* no FASYNC */
  838. #ifdef STRIDE        /* Stride doesn't have FASYNC - use FIOASYNC */
  839.  
  840. _VOID_
  841. request_sigio ()
  842. {
  843.   int on = 1;
  844.   ioctl (0, FIOASYNC, &on);
  845.   interrupts_deferred = 0;
  846. }
  847.  
  848. _VOID_
  849. unrequest_sigio ()
  850. {
  851.   int off = 0;
  852.  
  853.   ioctl (0, FIOASYNC, &off);
  854.   interrupts_deferred = 1;
  855. }
  856.  
  857. #else /* not FASYNC, not STRIDE */
  858.  
  859. _VOID_
  860. request_sigio ()
  861. {
  862.   croak ("request_sigio");
  863. }
  864.  
  865. _VOID_
  866. unrequest_sigio ()
  867. {
  868.   croak ("unrequest_sigio");
  869. }
  870.  
  871. #endif /* STRIDE */
  872. #endif /* FASYNC */
  873. #endif /* F_SETFL */
  874.  
  875. #ifdef WINDOWSNT
  876. /* Provide stub functions */
  877. _VOID_
  878. request_sigio()
  879. {
  880. }
  881.  
  882. _VOID_
  883. unrequest_sigio()
  884. {
  885. }
  886. #endif
  887.  
  888.  
  889. /* Saving and restoring the process group of Emacs's terminal.  */
  890.  
  891. #ifdef BSD
  892.  
  893. /* The process group of which Emacs was a member when it initially
  894.    started.
  895.  
  896.    If Emacs was in its own process group (i.e. inherited_pgroup ==
  897.    getpid ()), then we know we're running under a shell with job
  898.    control (Emacs would never be run as part of a pipeline).
  899.    Everything is fine.
  900.  
  901.    If Emacs was not in its own process group, then we know we're
  902.    running under a shell (or a caller) that doesn't know how to
  903.    separate itself from Emacs (like sh).  Emacs must be in its own
  904.    process group in order to receive SIGIO correctly.  In this
  905.    situation, we put ourselves in our own pgroup, forcibly set the
  906.    tty's pgroup to our pgroup, and make sure to restore and reinstate
  907.    the tty's pgroup just like any other terminal setting.  If
  908.    inherited_group was not the tty's pgroup, then we'll get a
  909.    SIGTTmumble when we try to change the tty's pgroup, and a CONT if
  910.    it goes foreground in the future, which is what should happen.  */
  911. int inherited_pgroup;
  912.  
  913. /* Split off the foreground process group to Emacs alone.
  914.    When we are in the foreground, but not started in our own process
  915.    group, redirect the TTY to point to our own process group.  We need
  916.    to be in our own process group to receive SIGIO properly.  */
  917. _VOID_
  918. narrow_foreground_group ()
  919. {
  920.   int me = getpid ();
  921.  
  922.   setpgrp (0, inherited_pgroup);
  923.   if (inherited_pgroup != me)
  924.     EMACS_SET_TTY_PGRP (0, &me);
  925.   setpgrp (0, me);
  926. }
  927.  
  928. /* Set the tty to our original foreground group.  */
  929. _VOID_
  930. widen_foreground_group ()
  931. {
  932.   if (inherited_pgroup != getpid ())
  933.     EMACS_SET_TTY_PGRP (0, &inherited_pgroup);
  934.   setpgrp (0, inherited_pgroup);
  935. }
  936.  
  937. #endif
  938.  
  939. /* Getting and setting emacs_tty structures.  */
  940.  
  941. /* Set *TC to the parameters associated with the terminal FD.
  942.    Return zero if all's well, or -1 if we ran into an error we
  943.    couldn't deal with.  */
  944. int
  945. emacs_get_tty (fd, settings)
  946.      int fd;
  947.      struct emacs_tty *settings;
  948. {
  949.   /* Retrieve the primary parameters - baud rate, character size, etcetera.  */
  950. #ifdef HAVE_TCATTR
  951.   /* We have those nifty POSIX tcmumbleattr functions.  */
  952.   if (tcgetattr (fd, &settings->main) < 0)
  953.     return -1;
  954.  
  955. #else
  956. #ifdef HAVE_TERMIO
  957.   /* The SYSV-style interface?  */
  958.   if (ioctl (fd, TCGETA, &settings->main) < 0)
  959.     return -1;
  960.  
  961. #else
  962. #ifdef VMS
  963.   /* Vehemently Monstrous System?  :-)  */
  964.   if (! (SYS$QIOW (0, fd, IO$_SENSEMODE, settings, 0, 0,
  965.            &settings->main.class, 12, 0, 0, 0, 0)
  966.      & 1))
  967.     return -1;
  968.  
  969. #else
  970. #ifdef WINDOWSNT
  971.   /*
  972.    * emacs_tty currently has no meaning on NT.
  973.    */
  974.   settings->main = 0;
  975. #else
  976.   /* I give up - I hope you have the BSD ioctls.  */
  977.   if (ioctl (fd, TIOCGETP, &settings->main) < 0)
  978.     return -1;
  979. #endif /* !WINDOWSNT */
  980. #endif
  981. #endif
  982. #endif
  983.  
  984.   /* Suivant - Do we have to get struct ltchars data?  */
  985. #ifdef HAVE_LTCHARS
  986.   if (ioctl (fd, TIOCGLTC, &settings->ltchars) < 0)
  987.     return -1;
  988. #endif
  989.  
  990.   /* How about a struct tchars and a wordful of lmode bits?  */
  991. #ifdef HAVE_TCHARS
  992.   if (ioctl (fd, TIOCGETC, &settings->tchars) < 0
  993.       || ioctl (fd, TIOCLGET, &settings->lmode) < 0)
  994.     return -1;
  995. #endif
  996.  
  997.   /* We have survived the tempest.  */
  998.   return 0;
  999. }
  1000.  
  1001.  
  1002. /* Set the parameters of the tty on FD according to the contents of
  1003.    *SETTINGS.  If WAITP is non-zero, we wait for all queued output to
  1004.    be written before making the change; otherwise, we forget any
  1005.    queued input and make the change immediately.
  1006.    Return 0 if all went well, and -1 if anything failed.  */
  1007. int
  1008. emacs_set_tty (fd, settings, waitp)
  1009.      int fd;
  1010.      struct emacs_tty *settings;
  1011.      int waitp;
  1012. {
  1013.   /* Set the primary parameters - baud rate, character size, etcetera.  */
  1014. #ifdef HAVE_TCATTR
  1015.   int i;
  1016.   /* We have those nifty POSIX tcmumbleattr functions.
  1017.      William J. Smith <wjs@wiis.wang.com> writes:
  1018.      "POSIX 1003.1 defines tcsetattr() to return success if it was
  1019.      able to perform any of the requested actions, even if some
  1020.      of the requested actions could not be performed.
  1021.      We must read settings back to ensure tty setup properly.
  1022.      AIX requires this to keep tty from hanging occasionally."  */
  1023.   /* This make sure that we don't loop indefinitely in here.  */
  1024.   for (i = 0 ; i < 10 ; i++)
  1025.     if (tcsetattr (fd, waitp ? TCSAFLUSH : TCSADRAIN, &settings->main) < 0)
  1026.       {
  1027.     if (errno == EINTR)
  1028.       continue;
  1029.     else
  1030.       return -1;
  1031.       }
  1032.     else
  1033.       {
  1034.     struct termios new;
  1035.  
  1036.     /* Get the current settings, and see if they're what we asked for.  */
  1037.     tcgetattr (fd, &new);
  1038.     /* We cannot use memcmp on the whole structure here because under
  1039.      * aix386 the termios structure has some reserved field that may
  1040.      * not be filled in.
  1041.      */
  1042.     if (   new.c_iflag == settings->main.c_iflag
  1043.         && new.c_oflag == settings->main.c_oflag
  1044.         && new.c_cflag == settings->main.c_cflag
  1045.         && new.c_lflag == settings->main.c_lflag
  1046.         && memcmp(new.c_cc, settings->main.c_cc, NCCS) == 0)
  1047.       break;
  1048.     else
  1049.       continue;
  1050.       }
  1051.  
  1052. #else
  1053. #ifdef HAVE_TERMIO
  1054.   /* The SYSV-style interface?  */
  1055.   if (ioctl (fd, waitp ? TCSETAW : TCSETAF, &settings->main) < 0)
  1056.     return -1;
  1057.  
  1058. #else
  1059. #ifdef VMS
  1060.   /* Vehemently Monstrous System?  :-)  */
  1061.   if (! (SYS$QIOW (0, fd, IO$_SETMODE, &input_iosb, 0, 0,
  1062.            &settings->main.class, 12, 0, 0, 0, 0)
  1063.      & 1))
  1064.     return -1;
  1065.  
  1066. #else
  1067. #ifdef WINDOWSNT
  1068.   /*
  1069.    * A no-op on NT.
  1070.    */
  1071. #else
  1072.   /* I give up - I hope you have the BSD ioctls.  */
  1073.   if (ioctl (fd, (waitp) ? TIOCSETP : TIOCSETN, &settings->main) < 0)
  1074.     return -1;
  1075. #endif /* !WINDOWSNT */
  1076. #endif
  1077. #endif
  1078. #endif
  1079.  
  1080.   /* Suivant - Do we have to get struct ltchars data?  */
  1081. #ifdef HAVE_LTCHARS
  1082.   if (ioctl (fd, TIOCSLTC, &settings->ltchars) < 0)
  1083.     return -1;
  1084. #endif
  1085.  
  1086.   /* How about a struct tchars and a wordful of lmode bits?  */
  1087. #ifdef HAVE_TCHARS
  1088.   if (ioctl (fd, TIOCSETC, &settings->tchars) < 0
  1089.       || ioctl (fd, TIOCLSET, &settings->lmode) < 0)
  1090.     return -1;
  1091. #endif
  1092.   
  1093.   /* We have survived the tempest.  */
  1094.   return 0;
  1095. }
  1096.  
  1097.  
  1098. /* The initial tty mode bits */
  1099. struct emacs_tty old_tty;
  1100.  
  1101. int term_initted;        /* 1 if outer tty status has been recorded */
  1102.  
  1103. #ifdef BSD4_1
  1104. /* BSD 4.1 needs to keep track of the lmode bits in order to start
  1105.    sigio.  */
  1106. int lmode;
  1107. #endif
  1108.  
  1109. #ifndef F_SETOWN_BUG
  1110. #ifdef F_SETOWN
  1111. int old_fcntl_owner;
  1112. #endif /* F_SETOWN */
  1113. #endif /* F_SETOWN_BUG */
  1114.  
  1115. /* This may also be defined in stdio,
  1116.    but if so, this does no harm,
  1117.    and using the same name avoids wasting the other one's space.  */
  1118.  
  1119. #if defined (USG) || defined (DGUX)
  1120. unsigned char _sobuf[BUFSIZ+8];
  1121. #else
  1122. char _sobuf[BUFSIZ];
  1123. #endif
  1124.  
  1125. #ifdef HAVE_LTCHARS
  1126. static struct ltchars new_ltchars = {-1,-1,-1,-1,-1,-1};
  1127. #endif
  1128. #ifdef HAVE_TCHARS
  1129.   static struct tchars new_tchars = {-1,-1,-1,-1,-1,-1};
  1130. #endif 
  1131.  
  1132. /* This routine is used in record_unwind_protect so it needs to look
  1133.    like a Lisp function */
  1134. Lisp_Object
  1135. init_sys_modes (dummy)
  1136.     Lisp_Object dummy;
  1137. {
  1138. #ifdef WINDOWSNT
  1139.     /*
  1140.      * I don't know what I'm doing here, but compare to the "#ifdef soup"
  1141.      * below it's beautiful.
  1142.      */
  1143.  
  1144.     set_terminal_modes();
  1145.  
  1146.     if (term_initted && no_redraw_on_reenter) {
  1147.         if (display_completed)
  1148.             direct_output_forward_char (0);
  1149.     } else
  1150.         frame_garbaged = 1;
  1151.  
  1152.     term_initted = 1;
  1153.  
  1154.     return Qt;
  1155. #else /* !WINDOWSNT */
  1156.   struct emacs_tty tty;
  1157.  
  1158. #ifdef VMS
  1159. #if 0
  1160.   static int oob_chars[2] = {0, 1 << 7}; /* catch C-g's */
  1161.   extern int (*interrupt_signal) ();
  1162. #endif
  1163. #endif
  1164.  
  1165.   if (noninteractive)
  1166.     return Qt;
  1167.  
  1168. #ifdef VMS
  1169.   if (!input_ef)
  1170.     input_ef = get_kbd_event_flag ();
  1171.     /* LIB$GET_EF (&input_ef); */
  1172.   SYS$CLREF (input_ef);
  1173.   waiting_for_ast = 0;
  1174.   if (!timer_ef)
  1175.     timer_ef = get_timer_event_flag ();
  1176.     /* LIB$GET_EF (&timer_ef); */
  1177.   SYS$CLREF (timer_ef);
  1178. #if 0
  1179.   if (!process_ef)
  1180.     {
  1181.       LIB$GET_EF (&process_ef);
  1182.       SYS$CLREF (process_ef);
  1183.     }
  1184.   if (input_ef / 32 != process_ef / 32)
  1185.     croak ("Input and process event flags in different clusters.");
  1186. #endif
  1187.   if (input_ef / 32 != timer_ef / 32)
  1188.     croak ("Input and timer event flags in different clusters.");
  1189. #if 0
  1190.   input_eflist = ((unsigned) 1 << (input_ef % 32)) |
  1191.     ((unsigned) 1 << (process_ef % 32));
  1192. #endif
  1193.   timer_eflist = ((unsigned) 1 << (input_ef % 32)) |
  1194.     ((unsigned) 1 << (timer_ef % 32));
  1195. #ifndef VMS4_4
  1196.   sys_access_reinit ();
  1197. #endif
  1198. #endif /* not VMS */
  1199.  
  1200. #ifdef BSD
  1201.   if (! read_socket_hook && EQ (Vwindow_system, Qnil))
  1202.     narrow_foreground_group ();
  1203. #endif
  1204.  
  1205.   EMACS_GET_TTY (input_fd, &old_tty);
  1206.  
  1207.   if (!read_socket_hook && EQ (Vwindow_system, Qnil))
  1208.     {
  1209.       tty = old_tty;
  1210.  
  1211. #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
  1212.       tty.main.c_iflag |= (IGNBRK);    /* Ignore break condition */
  1213.       tty.main.c_iflag &= ~ICRNL;    /* Disable map of CR to NL on input */
  1214. #ifdef ISTRIP
  1215.       tty.main.c_iflag &= ~ISTRIP;    /* don't strip 8th bit on input */
  1216. #endif
  1217.       tty.main.c_lflag &= ~ECHO;    /* Disable echo */
  1218.       tty.main.c_lflag &= ~ICANON;    /* Disable erase/kill processing */
  1219. #ifdef IEXTEN
  1220.       tty.main.c_iflag &= ~IEXTEN;    /* Disable other editing characters.  */
  1221. #endif
  1222.       tty.main.c_lflag |= ISIG;    /* Enable signals */
  1223.       if (flow_control)
  1224.     {
  1225.       tty.main.c_iflag |= IXON;    /* Enable start/stop output control */
  1226. #ifdef IXANY
  1227.       tty.main.c_iflag &= ~IXANY;
  1228. #endif /* IXANY */
  1229.     }
  1230.       else
  1231.     tty.main.c_iflag &= ~IXON;    /* Disable start/stop output control */
  1232.       tty.main.c_oflag &= ~ONLCR;    /* Disable map of NL to CR-NL 
  1233.                        on output */
  1234.       tty.main.c_oflag &= ~TAB3;    /* Disable tab expansion */
  1235. #ifdef CS8
  1236.       if (meta_key)
  1237.     {
  1238.       tty.main.c_cflag |= CS8;    /* allow 8th bit on input */
  1239.       tty.main.c_cflag &= ~PARENB;/* Don't check parity */
  1240.     }
  1241. #endif
  1242.       tty.main.c_cc[VINTR] = quit_char;    /* C-g (usually) gives SIGINT */
  1243.       /* Set up C-g for both SIGQUIT and SIGINT.
  1244.      We don't know which we will get, but we handle both alike
  1245.      so which one it really gives us does not matter.  */
  1246.       tty.main.c_cc[VQUIT] = quit_char;
  1247.       tty.main.c_cc[VMIN] = 1;    /* Input should wait for at least 1 char */
  1248.       tty.main.c_cc[VTIME] = 0;    /* no matter how long that takes.  */
  1249. #ifdef VSWTCH
  1250.       tty.main.c_cc[VSWTCH] = CDISABLE;    /* Turn off shell layering use
  1251.                        of C-z */
  1252. #endif /* VSWTCH */
  1253. #if defined (mips) || defined (HAVE_TCATTR)
  1254. #ifdef VSUSP
  1255.       tty.main.c_cc[VSUSP] = CDISABLE;    /* Turn off mips handling of C-z.  */
  1256. #endif /* VSUSP */
  1257. #ifdef V_DSUSP
  1258.       tty.main.c_cc[V_DSUSP] = CDISABLE; /* Turn off mips handling of C-y.  */
  1259. #endif /* V_DSUSP */
  1260. #ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP.  */
  1261.       tty.main.c_cc[VDSUSP] = CDISABLE;
  1262. #endif /* VDSUSP */
  1263. #ifdef VLNEXT
  1264.       tty.main.c_cc[VLNEXT] = CDISABLE;
  1265. #endif /* VLNEXT */
  1266. #ifdef VREPRINT
  1267.       tty.main.c_cc[VREPRINT] = CDISABLE;
  1268. #endif /* VREPRINT */
  1269. #ifdef VWERASE
  1270.       tty.main.c_cc[VWERASE] = CDISABLE;
  1271. #endif /* VWERASE */
  1272. #ifdef VDISCARD
  1273.       tty.main.c_cc[VDISCARD] = CDISABLE;
  1274. #endif /* VDISCARD */
  1275. #endif /* mips or HAVE_TCATTR */
  1276. #ifdef AIX
  1277. #ifndef IBMR2AIX
  1278.       /* AIX enhanced edit loses NULs, so disable it */
  1279.       tty.main.c_line = 0;
  1280.       tty.main.c_iflag &= ~ASCEDIT;
  1281. #else
  1282.       tty.main.c_cc[VSTRT] = 255;
  1283.       tty.main.c_cc[VSTOP] = 255;
  1284.       tty.main.c_cc[VSUSP] = 255;
  1285.       tty.main.c_cc[VDSUSP] = 255;
  1286. #endif /* IBMR2AIX */
  1287.       /* Also, PTY overloads NUL and BREAK.
  1288.      don't ignore break, but don't signal either, so it looks like NUL.
  1289.      This really serves a purpose only if running in an XTERM window
  1290.      or via TELNET or the like, but does no harm elsewhere.  */
  1291.       tty.main.c_iflag &= ~IGNBRK;
  1292.       tty.main.c_iflag &= ~BRKINT;
  1293. #endif
  1294. #else /* if not HAVE_TERMIO */
  1295. #ifdef VMS
  1296.       tty.main.tt_char |= TT$M_NOECHO;
  1297.       if (meta_key)
  1298.     tty.main.tt_char |= TT$M_EIGHTBIT;
  1299.       if (flow_control)
  1300.     tty.main.tt_char |= TT$M_TTSYNC;
  1301.       else
  1302.     tty.main.tt_char &= ~TT$M_TTSYNC;
  1303.       tty.main.tt2_char |= TT2$M_PASTHRU | TT2$M_XON;
  1304. #else /* not VMS (BSD, that is) */
  1305.       tty.main.sg_flags &= ~(ECHO | CRMOD | XTABS);
  1306.       if (meta_key)
  1307.     tty.main.sg_flags |= ANYP;
  1308.       tty.main.sg_flags |= interrupt_input ? RAW : CBREAK;
  1309. #endif /* not VMS (BSD, that is) */
  1310. #endif /* not HAVE_TERMIO */
  1311.  
  1312.       /* If going to use CBREAK mode, we must request C-g to interrupt
  1313.      and turn off start and stop chars, etc.  If not going to use
  1314.      CBREAK mode, do this anyway so as to turn off local flow
  1315.      control for user coming over network on 4.2; in this case,
  1316.      only t_stopc and t_startc really matter.  */
  1317. #ifndef HAVE_TERMIO
  1318. #ifdef HAVE_TCHARS
  1319.       /* Note: if not using CBREAK mode, it makes no difference how we
  1320.      set this */
  1321.       tty.tchars = new_tchars;
  1322.       tty.tchars.t_intrc = quit_char;
  1323.       if (flow_control)
  1324.     {
  1325.       tty.tchars.t_startc = '\021';
  1326.       tty.tchars.t_stopc = '\023';
  1327.     }
  1328.  
  1329. /* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits.  */
  1330. #ifndef LPASS8
  1331. #define LPASS8 0
  1332. #endif
  1333.  
  1334. #ifdef BSD4_1
  1335. #define LNOFLSH 0100000
  1336. #endif
  1337.  
  1338.       tty.lmode = LDECCTQ | LLITOUT | LPASS8 | LNOFLSH | old_tty.lmode;
  1339. #ifdef ultrix
  1340.       /* Under Ultrix 4.2a, leaving this out doesn't seem to hurt
  1341.      anything, and leaving it in breaks the meta key.  Go figure.  */
  1342.       tty.lmode &= ~LLITOUT;
  1343. #endif
  1344.       
  1345. #ifdef BSD4_1
  1346.       lmode = tty.lmode;
  1347. #endif
  1348.  
  1349. #endif /* HAVE_TCHARS */
  1350. #endif /* not HAVE_TERMIO */
  1351.  
  1352. #ifdef HAVE_LTCHARS
  1353.       tty.ltchars = new_ltchars;
  1354. #endif /* HAVE_LTCHARS */
  1355.  
  1356.       EMACS_SET_TTY (input_fd, &tty, 0);
  1357.  
  1358.       /* This code added to insure that, if flow-control is not to be used,
  1359.      we have an unlocked terminal at the start. */
  1360.  
  1361. #ifdef TCXONC
  1362.       if (!flow_control) ioctl (0, TCXONC, 1);
  1363. #endif
  1364. #ifndef APOLLO
  1365. #ifdef TIOCSTART
  1366.       if (!flow_control) ioctl (0, TIOCSTART, 0);
  1367. #endif
  1368. #endif
  1369.  
  1370. #ifdef AIX
  1371.       hft_init ();
  1372. #ifdef IBMR2AIX
  1373.       {
  1374.     /* IBM's HFT device usually thinks a ^J should be LF/CR.  We need it
  1375.        to be only LF.  This is the way that is done. */
  1376.     struct termio tty;
  1377.  
  1378.     if (ioctl (1, HFTGETID, &tty) != -1)
  1379.       write (1, "\033[20l", 5);
  1380.       }
  1381. #endif
  1382. #endif
  1383.  
  1384. #ifdef VMS
  1385. /*  Appears to do nothing when in PASTHRU mode.
  1386.       SYS$QIOW (0, input_fd, IO$_SETMODE|IO$M_OUTBAND, 0, 0, 0,
  1387.         interrupt_signal, oob_chars, 0, 0, 0, 0);
  1388. */
  1389.       queue_kbd_input (0);
  1390. #endif /* VMS */
  1391.     }
  1392.  
  1393. #ifdef F_SETFL
  1394. #ifndef F_SETOWN_BUG
  1395. #ifdef F_GETOWN        /* F_SETFL does not imply existence of F_GETOWN */
  1396.   if (interrupt_input)
  1397.     {
  1398.       old_fcntl_owner = fcntl (0, F_GETOWN, 0);
  1399.       fcntl (0, F_SETOWN, getpid ());
  1400.       init_sigio ();
  1401.     }
  1402. #endif /* F_GETOWN */
  1403. #endif /* F_SETOWN_BUG */
  1404. #endif /* F_SETFL */
  1405.  
  1406. #ifdef BSD4_1
  1407.   if (interrupt_input)
  1408.     init_sigio ();
  1409. #endif
  1410.  
  1411. #ifdef VMS  /* VMS sometimes has this symbol but lacks setvbuf.  */
  1412. #undef _IOFBF
  1413. #endif
  1414. #ifdef _IOFBF
  1415.   /* This symbol is defined on recent USG systems.
  1416.      Someone says without this call USG won't really buffer the file
  1417.      even with a call to setbuf. */
  1418.   setvbuf (stdout, _sobuf, _IOFBF, sizeof _sobuf);
  1419. #else
  1420.   setbuf (stdout, _sobuf);
  1421. #endif
  1422.   set_terminal_modes ();
  1423.   if (term_initted && no_redraw_on_reenter)
  1424.     {
  1425.       if (display_completed)
  1426.     direct_output_forward_char (0);
  1427.     }
  1428.   else
  1429.     {
  1430.       frame_garbaged = 1;
  1431. #ifdef MULTI_FRAME
  1432.       if (FRAMEP (Vterminal_frame))
  1433.     FRAME_GARBAGED_P (XFRAME (Vterminal_frame)) = 1;
  1434. #endif
  1435.     }
  1436.  
  1437.   term_initted = 1;
  1438.   return Qt;
  1439. #endif /* WINDOWSNT */
  1440. }
  1441.  
  1442. /* Return nonzero if safe to use tabs in output.
  1443.    At the time this is called, init_sys_modes has not been done yet.  */
  1444.  
  1445. int
  1446. tabs_safe_p ()
  1447. {
  1448. #ifdef    WINDOWSNT
  1449.   /* Though I don't think this is ever called... */
  1450.   return 0;
  1451. #else /* !WINDOWSNT */
  1452.   struct emacs_tty tty;
  1453.  
  1454.   EMACS_GET_TTY (input_fd, &tty);
  1455.   return EMACS_TTY_TABS_OK (&tty);
  1456. #endif /* !WINDOWSNT */
  1457. }
  1458.  
  1459. /* Get terminal size from system.
  1460.    Store number of lines into *heightp and width into *widthp.
  1461.    If zero or a negative number is stored, the value is not valid.  */
  1462.  
  1463. _VOID_
  1464. get_frame_size (widthp, heightp)
  1465.      int *widthp, *heightp;
  1466. {
  1467.  
  1468. #ifdef TIOCGWINSZ
  1469.  
  1470.   /* BSD-style.  */
  1471.   struct winsize size;
  1472.  
  1473.   if (ioctl (input_fd, TIOCGWINSZ, &size) == -1)
  1474.     *widthp = *heightp = 0;
  1475.   else
  1476.     {
  1477.       *widthp = size.ws_col;
  1478.       *heightp = size.ws_row;
  1479.     }
  1480.  
  1481. #else
  1482. #ifdef TIOCGSIZE
  1483.  
  1484.   /* SunOS - style.  */
  1485.   struct ttysize size;  
  1486.  
  1487.   if (ioctl (input_fd, TIOCGSIZE, &size) == -1)
  1488.     *widthp = *heightp = 0;
  1489.   else
  1490.     {
  1491.       *widthp = size.ts_cols;
  1492.       *heightp = size.ts_lines;
  1493.     }
  1494.  
  1495. #else
  1496. #ifdef VMS
  1497.  
  1498.   struct sensemode tty;
  1499.   
  1500.   SYS$QIOW (0, input_fd, IO$_SENSEMODE, &tty, 0, 0,
  1501.         &tty.class, 12, 0, 0, 0, 0);
  1502.   *widthp = tty.scr_wid;
  1503.   *heightp = tty.scr_len;
  1504.  
  1505. #else /* system doesn't know size */
  1506.  
  1507.   *widthp = 0;
  1508.   *heightp = 0;
  1509.  
  1510. #endif /* not VMS */
  1511. #endif /* not SunOS-style */
  1512. #endif /* not BSD-style */
  1513. }
  1514.  
  1515.  
  1516. /* Prepare the terminal for exiting Emacs; move the cursor to the
  1517.    bottom of the frame, turn off interrupt-driven I/O, etc.  */
  1518. _VOID_
  1519. reset_sys_modes ()
  1520. {
  1521.   if (noninteractive)
  1522.     {
  1523.       fflush (stdout);
  1524.       return;
  1525.     }
  1526.   if (!term_initted)
  1527.     return;
  1528.   if (read_socket_hook || !EQ (Vwindow_system, Qnil))
  1529.     return;
  1530.   cursor_to (FRAME_HEIGHT (selected_frame) - 1, 0);
  1531.   clear_end_of_line (FRAME_WIDTH (selected_frame));
  1532.   /* clear_end_of_line may move the cursor */
  1533.   cursor_to (FRAME_HEIGHT (selected_frame) - 1, 0);
  1534. #ifdef IBMR2AIX
  1535.   {
  1536.     /* HFT devices normally use ^J as a LF/CR.  We forced it to 
  1537.        do the LF only.  Now, we need to reset it. */
  1538.     struct termio tty;
  1539.  
  1540.     if (ioctl (1, HFTGETID, &tty) != -1)
  1541.       write (1, "\033[20h", 5);
  1542.   }
  1543. #endif
  1544.  
  1545.   reset_terminal_modes ();
  1546.   fflush (stdout);
  1547.  
  1548. #ifndef WINDOWSNT
  1549. #ifdef BSD
  1550. #ifndef BSD4_1
  1551.   /* Avoid possible loss of output when changing terminal modes.  */
  1552.   fsync (fileno (stdout));
  1553. #endif
  1554. #endif
  1555.  
  1556. #ifdef F_SETFL
  1557. #ifndef F_SETOWN_BUG
  1558. #ifdef F_SETOWN        /* F_SETFL does not imply existence of F_SETOWN */
  1559.   if (interrupt_input)
  1560.     {
  1561.       reset_sigio ();
  1562.       fcntl (0, F_SETOWN, old_fcntl_owner);
  1563.     }
  1564. #endif /* F_SETOWN */
  1565. #endif /* F_SETOWN_BUG */
  1566. #endif /* F_SETFL */
  1567. #ifdef BSD4_1
  1568.   if (interrupt_input)
  1569.     reset_sigio ();
  1570. #endif /* BSD4_1 */
  1571.  
  1572.   while (EMACS_SET_TTY (input_fd, &old_tty, 0) < 0 && errno == EINTR)
  1573.     ;
  1574.  
  1575. #ifdef AIX
  1576.   hft_reset ();
  1577. #endif
  1578.  
  1579. #ifdef BSD
  1580.   widen_foreground_group ();
  1581. #endif
  1582. #endif /* !WINDOWSNT */
  1583. }
  1584.  
  1585. #ifdef HAVE_PTYS
  1586.  
  1587. /* Set up the proper status flags for use of a pty.  */
  1588.  
  1589. _VOID_
  1590. setup_pty (fd)
  1591.      int fd;
  1592. {
  1593.   /* I'm told that TOICREMOTE does not mean control chars
  1594.      "can't be sent" but rather that they don't have
  1595.      input-editing or signaling effects.
  1596.      That should be good, because we have other ways
  1597.      to do those things in Emacs.
  1598.      However, telnet mode seems not to work on 4.2.
  1599.      So TIOCREMOTE is turned off now. */
  1600.  
  1601.   /* Under hp-ux, if TIOCREMOTE is turned on, some calls
  1602.      will hang.  In particular, the "timeout" feature (which
  1603.      causes a read to return if there is no data available)
  1604.      does this.  Also it is known that telnet mode will hang
  1605.      in such a way that Emacs must be stopped (perhaps this
  1606.      is the same problem).
  1607.      
  1608.      If TIOCREMOTE is turned off, then there is a bug in
  1609.      hp-ux which sometimes loses data.  Apparently the
  1610.      code which blocks the master process when the internal
  1611.      buffer fills up does not work.  Other than this,
  1612.      though, everything else seems to work fine.
  1613.      
  1614.      Since the latter lossage is more benign, we may as well
  1615.      lose that way.  -- cph */
  1616. #ifdef FIONBIO
  1617. #ifdef SYSV_PTYS
  1618.   {
  1619.     int on = 1;
  1620.     ioctl (fd, FIONBIO, &on);
  1621.   }
  1622. #endif
  1623. #endif
  1624. #ifdef IBMRTAIX
  1625.   /* On AIX, the parent gets SIGHUP when a pty attached child dies.  So, we */
  1626.   /* ignore SIGHUP once we've started a child on a pty.  Note that this may */
  1627.   /* cause EMACS not to die when it should, i.e., when its own controlling  */
  1628.   /* tty goes away.  I've complained to the AIX developers, and they may    */
  1629.   /* change this behavior, but I'm not going to hold my breath.             */
  1630.   signal (SIGHUP, SIG_IGN);
  1631. #endif
  1632. }
  1633. #endif /* HAVE_PTYS */
  1634.  
  1635. #ifdef VMS
  1636.  
  1637. /* Assigning an input channel is done at the start of Emacs execution.
  1638.    This is called each time Emacs is resumed, also, but does nothing
  1639.    because input_chain is no longer zero.  */
  1640.  
  1641. _VOID_
  1642. init_vms_input ()
  1643. {
  1644.   int status;
  1645.   
  1646.   if (input_fd == 0)
  1647.     {
  1648.       status = SYS$ASSIGN (&input_dsc, &input_fd, 0, 0);
  1649.       if (! (status & 1))
  1650.     LIB$STOP (status);
  1651.     }
  1652. }
  1653.  
  1654. /* Deassigning the input channel is done before exiting.  */
  1655.  
  1656. _VOID_
  1657. stop_vms_input ()
  1658. {
  1659.   return SYS$DASSGN (input_fd);
  1660. }
  1661.  
  1662. short input_buffer;
  1663.  
  1664. /* Request reading one character into the keyboard buffer.
  1665.    This is done as soon as the buffer becomes empty.  */
  1666.  
  1667. _VOID_
  1668. queue_kbd_input ()
  1669. {
  1670.   int status;
  1671.   extern kbd_input_ast ();
  1672.  
  1673.   waiting_for_ast = 0;
  1674.   stop_input = 0;
  1675.   status = SYS$QIO (0, input_fd, IO$_READVBLK,
  1676.             &input_iosb, kbd_input_ast, 1,
  1677.             &input_buffer, 1, 0, terminator_mask, 0, 0);
  1678. }
  1679.  
  1680. int input_count;
  1681.  
  1682. /* Ast routine that is called when keyboard input comes in
  1683.    in accord with the SYS$QIO above.  */
  1684.  
  1685. _VOID_
  1686. kbd_input_ast ()
  1687. {
  1688.   register int c = -1;
  1689.   int old_errno = errno;
  1690.   extern EMACS_TIME *input_available_clear_time;
  1691.  
  1692.   if (waiting_for_ast)
  1693.     SYS$SETEF (input_ef);
  1694.   waiting_for_ast = 0;
  1695.   input_count++;
  1696. #ifdef ASTDEBUG
  1697.   if (input_count == 25)
  1698.     exit (1);
  1699.   printf ("Ast # %d,", input_count);
  1700.   printf (" iosb = %x, %x, %x, %x",
  1701.       input_iosb.offset, input_iosb.status, input_iosb.termlen,
  1702.       input_iosb.term);
  1703. #endif
  1704.   if (input_iosb.offset)
  1705.     {
  1706.       c = input_buffer;
  1707. #ifdef ASTDEBUG
  1708.       printf (", char = 0%o", c);
  1709. #endif
  1710.     }
  1711. #ifdef ASTDEBUG
  1712.   printf ("\n");
  1713.   fflush (stdout);
  1714.   sleep (1);
  1715. #endif
  1716.   if (! stop_input)
  1717.     queue_kbd_input ();
  1718.   if (c >= 0)
  1719.     {
  1720.       struct input_event e;
  1721.       e.kind = ascii_keystroke;
  1722.       XSET (e.code, Lisp_Int, c);
  1723. #ifdef MULTI_FRAME
  1724.       XSET(e.frame_or_window, Lisp_Frame, selected_frame);
  1725. #else
  1726.       e.frame_or_window = Qnil;
  1727. #endif
  1728.       kbd_buffer_store_event (&e);
  1729.     }
  1730.   if (input_available_clear_time)
  1731.     EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
  1732.   errno = old_errno;
  1733. }
  1734.  
  1735. /* Wait until there is something in kbd_buffer.  */
  1736.  
  1737. _VOID_
  1738. wait_for_kbd_input ()
  1739. {
  1740.   extern int have_process_input, process_exited;
  1741.  
  1742.   /* If already something, avoid doing system calls.  */
  1743.   if (detect_input_pending ())
  1744.     {
  1745.       return;
  1746.     }
  1747.   /* Clear a flag, and tell ast routine above to set it.  */
  1748.   SYS$CLREF (input_ef);
  1749.   waiting_for_ast = 1;
  1750.   /* Check for timing error: ast happened while we were doing that.  */
  1751.   if (!detect_input_pending ())
  1752.     {
  1753.       /* No timing error: wait for flag to be set.  */
  1754.       set_waiting_for_input (0);
  1755.       SYS$WFLOR (input_ef, input_eflist);
  1756.       clear_waiting_for_input (0);
  1757.       if (!detect_input_pending ())
  1758.     /* Check for subprocess input availability */
  1759.     {
  1760.       int dsp = have_process_input || process_exited;
  1761.  
  1762.       SYS$CLREF (process_ef);
  1763.       if (have_process_input)
  1764.         process_command_input ();
  1765.       if (process_exited)
  1766.         process_exit ();
  1767.       if (dsp)
  1768.         {
  1769.           update_mode_lines++;
  1770.           redisplay_preserve_echo_area ();
  1771.         }
  1772.     }
  1773.     }
  1774.   waiting_for_ast = 0;
  1775. }
  1776.  
  1777. /* Get rid of any pending QIO, when we are about to suspend
  1778.    or when we want to throw away pending input.
  1779.    We wait for a positive sign that the AST routine has run
  1780.    and therefore there is no I/O request queued when we return.
  1781.    SYS$SETAST is used to avoid a timing error.  */
  1782.  
  1783. _VOID_
  1784. end_kbd_input ()
  1785. {
  1786. #ifdef ASTDEBUG
  1787.   printf ("At end_kbd_input.\n");
  1788.   fflush (stdout);
  1789.   sleep (1);
  1790. #endif
  1791.   if (LIB$AST_IN_PROG ())  /* Don't wait if suspending from kbd_buffer_store_event! */
  1792.     {
  1793.       SYS$CANCEL (input_fd);
  1794.       return;
  1795.     }
  1796.  
  1797.   SYS$SETAST (0);
  1798.   /* Clear a flag, and tell ast routine above to set it.  */
  1799.   SYS$CLREF (input_ef);
  1800.   waiting_for_ast = 1;
  1801.   stop_input = 1;
  1802.   SYS$CANCEL (input_fd);
  1803.   SYS$SETAST (1);
  1804.   SYS$WAITFR (input_ef);
  1805.   waiting_for_ast = 0;
  1806. }
  1807.  
  1808. /* Wait for either input available or time interval expiry.  */
  1809.  
  1810. _VOID_
  1811. input_wait_timeout (timeval)
  1812.      int timeval;        /* Time to wait, in seconds */
  1813. {
  1814.   int time [2];
  1815.   static int zero = 0;
  1816.   static int large = -10000000;
  1817.  
  1818.   LIB$EMUL (&timeval, &large, &zero, time);       /* Convert to VMS format */
  1819.  
  1820.   /* If already something, avoid doing system calls.  */
  1821.   if (detect_input_pending ())
  1822.     {
  1823.       return;
  1824.     }
  1825.   /* Clear a flag, and tell ast routine above to set it.  */
  1826.   SYS$CLREF (input_ef);
  1827.   waiting_for_ast = 1;
  1828.   /* Check for timing error: ast happened while we were doing that.  */
  1829.   if (!detect_input_pending ())
  1830.     {
  1831.       /* No timing error: wait for flag to be set.  */
  1832.       SYS$CANTIM (1, 0);
  1833.       if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
  1834.     SYS$WFLOR (timer_ef, timer_eflist);      /* Wait for timer expiry or input */
  1835.     }
  1836.   waiting_for_ast = 0;
  1837. }
  1838.  
  1839. /* The standard `sleep' routine works some other way
  1840.    and it stops working if you have ever quit out of it.
  1841.    This one continues to work.  */
  1842.  
  1843. _VOID_
  1844. sys_sleep (timeval)
  1845.      int timeval;
  1846. {
  1847.   int time [2];
  1848.   static int zero = 0;
  1849.   static int large = -10000000;
  1850.   
  1851.   LIB$EMUL (&timeval, &large, &zero, time);       /* Convert to VMS format */
  1852.  
  1853.   SYS$CANTIM (1, 0);
  1854.   if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
  1855.     SYS$WAITFR (timer_ef);      /* Wait for timer expiry only */
  1856. }
  1857.  
  1858. _VOID_
  1859. init_sigio ()
  1860. {
  1861.   request_sigio ();
  1862. }
  1863.  
  1864. _VOID_
  1865. reset_sigio ()
  1866. {
  1867.   unrequest_sigio ();
  1868. }
  1869.  
  1870. _VOID_
  1871. request_sigio ()
  1872. {
  1873.   croak ("request sigio");
  1874. }
  1875.  
  1876. _VOID_
  1877. unrequest_sigio ()
  1878. {
  1879.   croak ("unrequest sigio");
  1880. }
  1881.  
  1882. #endif /* VMS */
  1883.  
  1884. /* Note that VMS compiler won't accept defined (CANNOT_DUMP).  */
  1885. #ifndef CANNOT_DUMP
  1886. #define NEED_STARTS
  1887. #endif
  1888.  
  1889. #ifndef SYSTEM_MALLOC
  1890. #ifndef NEED_STARTS
  1891. #define NEED_STARTS
  1892. #endif
  1893. #endif
  1894.  
  1895. #ifdef NEED_STARTS
  1896. /* Some systems that cannot dump also cannot implement these.  */
  1897.  
  1898. /*
  1899.  *    Return the address of the start of the text segment prior to
  1900.  *    doing an unexec.  After unexec the return value is undefined.
  1901.  *    See crt0.c for further explanation and _start.
  1902.  *
  1903.  */
  1904.  
  1905. #ifndef CANNOT_UNEXEC
  1906. char *
  1907. start_of_text ()
  1908. {
  1909. #ifdef TEXT_START
  1910.   return ((char *) TEXT_START);
  1911. #else
  1912. #ifdef GOULD
  1913.   extern csrt ();
  1914.   return ((char *) csrt);
  1915. #else /* not GOULD */
  1916. #ifdef WINDOWSNT
  1917.   DebPrint(("EMACS broken @ "__FILE__ ": %d\n", __LINE__));
  1918. #else /* !WINDOWSNT */
  1919.   extern int _start ();
  1920.   return ((char *) _start);
  1921. #endif /* !WINDOWSNT */
  1922. #endif /* GOULD */
  1923. #endif /* TEXT_START */
  1924. }
  1925. #endif /* not CANNOT_UNEXEC */
  1926.  
  1927. /*
  1928.  *    Return the address of the start of the data segment prior to
  1929.  *    doing an unexec.  After unexec the return value is undefined.
  1930.  *    See crt0.c for further information and definition of data_start.
  1931.  *
  1932.  *    Apparently, on BSD systems this is etext at startup.  On
  1933.  *    USG systems (swapping) this is highly mmu dependent and
  1934.  *    is also dependent on whether or not the program is running
  1935.  *    with shared text.  Generally there is a (possibly large)
  1936.  *    gap between end of text and start of data with shared text.
  1937.  *
  1938.  *    On Uniplus+ systems with shared text, data starts at a
  1939.  *    fixed address.  Each port (from a given oem) is generally
  1940.  *    different, and the specific value of the start of data can
  1941.  *    be obtained via the UniPlus+ specific "uvar" system call,
  1942.  *    however the method outlined in crt0.c seems to be more portable.
  1943.  *
  1944.  *    Probably what will have to happen when a USG unexec is available,
  1945.  *    at least on UniPlus, is temacs will have to be made unshared so
  1946.  *    that text and data are contiguous.  Then once loadup is complete,
  1947.  *    unexec will produce a shared executable where the data can be
  1948.  *    at the normal shared text boundry and the startofdata variable
  1949.  *    will be patched by unexec to the correct value.
  1950.  *
  1951.  */
  1952.  
  1953. char *
  1954. start_of_data ()
  1955. {
  1956. #ifdef DATA_START
  1957.   return ((char *) DATA_START);
  1958. #else
  1959. #ifdef ORDINARY_LINK
  1960.   /*
  1961.    * This is a hack.  Since we're not linking crt0.c or pre_crt0.c,
  1962.    * data_start isn't defined.  We take the address of environ, which
  1963.    * is known to live at or near the start of the system crt0.c, and
  1964.    * we don't sweat the handful of bytes that might lose.
  1965.    */
  1966.   extern char **environ;
  1967.  
  1968.   return((char *) &environ);
  1969. #else
  1970. #ifdef    WINDOWSNT
  1971.   /*
  1972.    * See gmalloc.c:__default_morecore to see where this comes from. (voelker)
  1973.    */
  1974.   extern void *DataRegionBase;
  1975.  
  1976.   if (!DataRegionBase) {
  1977.       printf("Haven't allocated data region yet.\n");
  1978.       fflush(stdout);
  1979.       exit(1);
  1980.   }
  1981.  
  1982.   return DataRegionBase;
  1983.  
  1984. #else /* !WINDOWSNT */
  1985.   extern int data_start;
  1986.   return ((char *) &data_start);
  1987. #endif /* !WINDOWSNT */
  1988. #endif /* ORDINARY_LINK */
  1989. #endif /* DATA_START */
  1990. }
  1991. #endif /* NEED_STARTS (not CANNOT_DUMP or not SYSTEM_MALLOC) */
  1992.  
  1993. #ifndef CANNOT_DUMP
  1994. /* Some systems that cannot dump also cannot implement these.  */
  1995.  
  1996. /*
  1997.  *    Return the address of the end of the text segment prior to
  1998.  *    doing an unexec.  After unexec the return value is undefined.
  1999.  */
  2000.  
  2001. char *
  2002. end_of_text ()
  2003. {
  2004. #ifdef TEXT_END
  2005.   return ((char *) TEXT_END);
  2006. #else
  2007. #ifdef    WINDOWSNT
  2008.   DebPrint(("EMACS broken @ "__FILE__ ": %d\n", __LINE__));
  2009. #else /* !WINDOWSNT */
  2010.   extern int etext;
  2011.   return ((char *) &etext);
  2012. #endif /* !WINDOWSNT */
  2013. #endif
  2014. }
  2015.  
  2016. /*
  2017.  *    Return the address of the end of the data segment prior to
  2018.  *    doing an unexec.  After unexec the return value is undefined.
  2019.  */
  2020.  
  2021. char *
  2022. end_of_data ()
  2023. {
  2024. #ifdef DATA_END
  2025.   return ((char *) DATA_END);
  2026. #else
  2027. #ifdef WINDOWSNT
  2028.     /*
  2029.      * another fantasy
  2030.      */
  2031.     return    0x500000;
  2032. #else /* !WINDOWSNT */
  2033.   extern int edata;
  2034.   return ((char *) &edata);
  2035. #endif /* !WINDOWSNT */
  2036. #endif
  2037. }
  2038.  
  2039. #endif /* not CANNOT_DUMP */
  2040.  
  2041. /* Get_system_name returns as its value
  2042.  a string for the Lisp function system-name to return. */
  2043.  
  2044. #ifdef BSD4_1
  2045. #include <whoami.h>
  2046. #endif
  2047.  
  2048. /* Can't have this within the function since `static' is #defined to 
  2049.    nothing for some USG systems.  */
  2050. #ifdef USG
  2051. #ifdef HAVE_GETHOSTNAME
  2052. static char get_system_name_name[256];
  2053. #else /* not HAVE_GETHOSTNAME */
  2054. static struct utsname get_system_name_name;
  2055. #endif /* not HAVE_GETHOSTNAME */
  2056. #endif /* USG */
  2057.  
  2058. #ifndef BSD4_1
  2059. #ifndef USG
  2060. #ifndef VMS
  2061. #ifdef HAVE_SOCKETS
  2062. #include <sys/socket.h>
  2063. #include <netdb.h>
  2064. #endif /* HAVE_SOCKETS */
  2065. #endif /* not VMS */
  2066. #endif /* not USG */
  2067. #endif /* not BSD4_1 */
  2068.  
  2069. char *
  2070. get_system_name ()
  2071. {
  2072. #ifdef USG
  2073. #ifdef HAVE_GETHOSTNAME
  2074.   gethostname (get_system_name_name, sizeof (get_system_name_name));
  2075.   return get_system_name_name;
  2076. #else /* not HAVE_GETHOSTNAME */
  2077.   uname (&get_system_name_name);
  2078.   return (get_system_name_name.nodename);
  2079. #endif /* not HAVE_GETHOSTNAME */
  2080. #else /* Not USG */
  2081. #ifdef BSD4_1
  2082.   return sysname;
  2083. #else /* not USG, not 4.1 */
  2084.   static char system_name_saved[32];
  2085. #ifdef VMS
  2086.   char *sp;
  2087.   if ((sp = egetenv ("SYS$NODE")) == 0)
  2088.     sp = "vax-vms";
  2089.   else
  2090.     {
  2091.       char *end;
  2092.  
  2093.       if ((end = index (sp, ':')) != 0)
  2094.     *end = '\0';
  2095.     }
  2096.   strcpy (system_name_saved, sp);
  2097. #else /* not VMS */
  2098. #ifdef    WINDOWSNT
  2099. /* #pragma assert(MAX_COMPUTERNAME_LENGTH < sizeof(system_name_saved)) */
  2100.   {
  2101.     DWORD len = 32;
  2102.  
  2103.     if (!GetComputerName(system_name_saved, &len)) {
  2104.       strcpy(system_name_saved, "<unknown host>");
  2105.     }
  2106.   }
  2107. #else
  2108.   gethostname (system_name_saved, sizeof (system_name_saved));
  2109. #endif /* !WINDOWSNT */
  2110.  
  2111. #ifdef HAVE_SOCKETS
  2112.   /* Turn the hostname into the official, fully-qualified hostname.
  2113.      Don't do this if we're going to dump; this can confuse system
  2114.      libraries on some machines and make the dumped emacs core dump. */
  2115. #ifndef CANNOT_DUMP
  2116.   if (initialized)
  2117. #endif /* not CANNOT_DUMP */
  2118.     {
  2119.       struct hostent *hp;
  2120.       hp = gethostbyname (system_name_saved);
  2121.       if (hp && strlen (hp->h_name) < sizeof(system_name_saved))
  2122.     strcpy (system_name_saved, hp->h_name);
  2123.     }
  2124. #endif /* HAVE_SOCKETS */
  2125. #endif /* not VMS */
  2126.   return system_name_saved;
  2127. #endif /* not USG, not 4.1 */
  2128. #endif /* not USG */
  2129. }
  2130.  
  2131. #ifdef VMS
  2132. #ifndef HAVE_GETHOSTNAME
  2133. void
  2134. gethostname(buf, len)
  2135.     char *buf;
  2136.     int len;
  2137. {
  2138.     char *s;
  2139.     s = getenv ("SYS$NODE");
  2140.     if (s == NULL)
  2141.         buf[0] = '\0';
  2142.     else {
  2143.         strncpy (buf, s, len - 2);
  2144.         buf[len - 1] = '\0';
  2145.     } /* else */
  2146. } /* static void gethostname */
  2147. #endif /* ! HAVE_GETHOSTNAME */
  2148. #endif /* VMS */
  2149.  
  2150.  
  2151. #ifndef VMS
  2152. #ifndef HAVE_SELECT
  2153.  
  2154. #ifdef HAVE_X_WINDOWS
  2155. /* Cause explanatory error message at compile time,
  2156.    since the select emulation is not good enough for X.  */
  2157. int *x = &x_windows_lose_if_no_select_system_call;
  2158. #endif
  2159.  
  2160. /* Emulate as much as select as is possible under 4.1 and needed by Gnu Emacs
  2161.  * Only checks read descriptors.
  2162.  */
  2163. /* How long to wait between checking fds in select */
  2164. #define SELECT_PAUSE 1
  2165. int select_alarmed;
  2166.  
  2167. /* For longjmp'ing back to read_input_waiting.  */
  2168.  
  2169. jmp_buf read_alarm_throw;
  2170.  
  2171. /* Nonzero if the alarm signal should throw back to read_input_waiting.
  2172.    The read_socket_hook function sets this to 1 while it is waiting.  */
  2173.  
  2174. int read_alarm_should_throw;
  2175.  
  2176. SIGTYPE _CALLBACK_
  2177. select_alarm (sig)
  2178.     int sig;
  2179. {
  2180.   select_alarmed = 1;
  2181. #ifdef BSD4_1
  2182.   sigrelse (SIGALRM);
  2183. #else /* not BSD4_1 */
  2184.   signal (SIGALRM, SIG_IGN);
  2185. #endif /* not BSD4_1 */
  2186.   if (read_alarm_should_throw)
  2187.     longjmp (read_alarm_throw, 1);
  2188. }
  2189.  
  2190. #ifndef WINDOWSNT
  2191. /* Win32 version is in ntproc.c */
  2192.  
  2193. /* Only rfds are checked.  */
  2194. int
  2195. select (nfds, rfds, wfds, efds, timeout)
  2196.      int nfds;
  2197.      int *rfds, *wfds, *efds;
  2198.      int *timeout;
  2199. {
  2200.   int ravail = 0, orfds = 0, old_alarm;
  2201.   int timeoutval = timeout ? *timeout : 100000;
  2202.   int *local_timeout = &timeoutval;
  2203.   extern int proc_buffered_char[];
  2204. #ifndef subprocesses
  2205.   int process_tick = 0, update_tick = 0;
  2206. #else
  2207.   extern int process_tick, update_tick;
  2208. #endif
  2209.   SIGTYPE (*old_trap) ();
  2210.   unsigned char buf;
  2211.  
  2212.   if (rfds)
  2213.     {
  2214.       orfds = *rfds;
  2215.       *rfds = 0;
  2216.     }
  2217.   if (wfds)
  2218.     *wfds = 0;
  2219.   if (efds)
  2220.     *efds = 0;
  2221.  
  2222.   /* If we are looking only for the terminal, with no timeout,
  2223.      just read it and wait -- that's more efficient.  */
  2224.   if (orfds == 1 && *local_timeout == 100000 && process_tick == update_tick)
  2225.     {
  2226.       if (! detect_input_pending ())
  2227.     read_input_waiting ();
  2228.       *rfds = 1;
  2229.       return 1;
  2230.     }
  2231.  
  2232.   /* Once a second, till the timer expires, check all the flagged read
  2233.    * descriptors to see if any input is available.  If there is some then
  2234.    * set the corresponding bit in the return copy of rfds.
  2235.    */ 
  2236.   while (1)
  2237.     {
  2238.       register int to_check, bit, fd;
  2239.  
  2240.       if (rfds)
  2241.     {
  2242.       for (to_check = nfds, bit = 1, fd = 0; --to_check >= 0; bit <<= 1, fd++)
  2243.         {
  2244.           if (orfds & bit)
  2245.         {
  2246.           int avail = 0, status = 0;
  2247.  
  2248.           if (bit == 1)
  2249.             avail = detect_input_pending (); /* Special keyboard handler */
  2250.           else
  2251.             {
  2252. #ifdef FIONREAD
  2253.               status = ioctl (fd, FIONREAD, &avail);
  2254. #else /* no FIONREAD */
  2255.               /* Hoping it will return -1 if nothing available
  2256.              or 0 if all 0 chars requested are read.  */
  2257.               if (proc_buffered_char[fd] >= 0)
  2258.             avail = 1;
  2259.               else
  2260.             {
  2261.               avail = read (fd, &buf, 1);
  2262.               if (avail > 0)
  2263.                 proc_buffered_char[fd] = buf;
  2264.             }
  2265. #endif /* no FIONREAD */
  2266.             }
  2267.           if (status >= 0 && avail > 0)
  2268.             {
  2269.               (*rfds) |= bit;
  2270.               ravail++;
  2271.             }
  2272.         }
  2273.         }
  2274.     }
  2275.       if (*local_timeout == 0 || ravail != 0 || process_tick != update_tick)
  2276.     break;
  2277.       old_alarm = alarm (0);
  2278.       old_trap = signal (SIGALRM, select_alarm);
  2279.       select_alarmed = 0;
  2280.       alarm (SELECT_PAUSE);
  2281.       /* Wait for a SIGALRM (or maybe a SIGTINT) */
  2282.       while (select_alarmed == 0 && *local_timeout != 0
  2283.          && process_tick == update_tick)
  2284.     {
  2285.       /* If we are interested in terminal input,
  2286.          wait by reading the terminal.
  2287.          That makes instant wakeup for terminal input at least.  */
  2288.       if (orfds & 1)
  2289.         {
  2290.           read_input_waiting ();
  2291.           if (detect_input_pending ())
  2292.         select_alarmed = 1;
  2293.         }
  2294.       else
  2295.         pause ();
  2296.     }
  2297.       (*local_timeout) -= SELECT_PAUSE;
  2298.       /* Reset the old alarm if there was one */
  2299.       alarm (0);
  2300.       signal (SIGALRM, old_trap);
  2301.       if (old_alarm != 0)
  2302.     {
  2303.       /* Reset or forge an interrupt for the original handler. */
  2304.       old_alarm -= SELECT_PAUSE;
  2305.       if (old_alarm <= 0)
  2306.         kill (getpid (), SIGALRM); /* Fake an alarm with the orig' handler */
  2307.       else
  2308.         alarm (old_alarm);
  2309.     }
  2310.       if (*local_timeout == 0)  /* Stop on timer being cleared */
  2311.     break;
  2312.     }
  2313.   return ravail;
  2314. }
  2315. #endif
  2316.  
  2317. /* Read keyboard input into the standard buffer,
  2318.    waiting for at least one character.  */
  2319.  
  2320. /* Make all keyboard buffers much bigger when using X windows.  */
  2321. #ifdef HAVE_X_WINDOWS
  2322. #define BUFFER_SIZE_FACTOR 16
  2323. #else
  2324. #define BUFFER_SIZE_FACTOR 1
  2325. #endif
  2326.  
  2327. _VOID_
  2328. read_input_waiting ()
  2329. {
  2330.   char cbuf[256 * BUFFER_SIZE_FACTOR];
  2331.   struct input_event buf[256 * BUFFER_SIZE_FACTOR];
  2332.   int nread, i;
  2333.   extern int quit_char;
  2334.  
  2335.   if (read_socket_hook)
  2336.     {
  2337.       read_alarm_should_throw = 0;
  2338.       if (! setjmp (read_alarm_throw))
  2339.     nread = (*read_socket_hook) (0, buf, 256 * BUFFER_SIZE_FACTOR, 1, 0);
  2340.       else
  2341.     nread = -1;
  2342.     }
  2343.   else
  2344.   {
  2345. #ifdef    WINDOWSNT
  2346. #if 0
  2347.       /* Replaced by providing read_socket_hook */
  2348.     nread = read_nt_keyboard(cbuf, 1);
  2349. #else
  2350.     nread = 0;
  2351. #endif
  2352. #else /* !WINDOWSNT */
  2353.     nread = read (fileno (stdin), cbuf, 1);
  2354. #endif /* !WINDOWSNT */
  2355.  
  2356.     for (i = 0; i < nread; i++)
  2357.     {
  2358.         buf[i].kind = ascii_keystroke;
  2359.         buf[i].modifiers = 0;
  2360.         if (meta_key == 1 && (cbuf[i] & 0x80))
  2361.         buf[i].modifiers = meta_modifier;
  2362.         if (meta_key != 2)
  2363.         cbuf[i] &= ~0x80;
  2364.         
  2365.       XSET (buf[i].code,        Lisp_Int,   cbuf[i]);
  2366. #ifdef MULTI_FRAME
  2367.       XSET (buf[i].frame_or_window, Lisp_Frame, selected_frame);
  2368. #else
  2369.       buf[i].frame_or_window = Qnil;
  2370. #endif
  2371.     }
  2372.   }
  2373.   /* Scan the chars for C-g and store them in kbd_buffer.  */
  2374.   for (i = 0; i < nread; i++)
  2375.     {
  2376.       kbd_buffer_store_event (&buf[i]);
  2377.       /* Don't look at input that follows a C-g too closely.
  2378.      This reduces lossage due to autorepeat on C-g.  */
  2379.       if (buf[i].kind == ascii_keystroke && XINT(buf[i].code) == quit_char)
  2380.     break;
  2381.     }
  2382. }
  2383.  
  2384. #endif /* not HAVE_SELECT */
  2385. #endif /* not VMS */
  2386.  
  2387. #ifdef BSD4_1
  2388. /*
  2389.  * Partially emulate 4.2 open call.
  2390.  * open is defined as this in 4.1.
  2391.  *
  2392.  * - added by Michael Bloom @ Citicorp/TTI
  2393.  *
  2394.  */
  2395.  
  2396. int
  2397. sys_open (path, oflag, mode)
  2398.      char *path;
  2399.      int oflag, mode;
  2400. {
  2401.   if (oflag & O_CREAT) 
  2402.     return creat (path, mode);
  2403.   else
  2404.     return open (path, oflag);
  2405. }
  2406.  
  2407. _VOID_
  2408. init_sigio ()
  2409. {
  2410.   if (noninteractive)
  2411.     return;
  2412.   lmode = LINTRUP | lmode;
  2413.   ioctl (0, TIOCLSET, &lmode);
  2414. }
  2415.  
  2416. _VOID_
  2417. reset_sigio ()
  2418. {
  2419.   if (noninteractive)
  2420.     return;
  2421.   lmode = ~LINTRUP & lmode;
  2422.   ioctl (0, TIOCLSET, &lmode);
  2423. }
  2424.  
  2425. _VOID_
  2426. request_sigio ()
  2427. {
  2428.   sigrelse (SIGTINT);
  2429.  
  2430.   interrupts_deferred = 0;
  2431. }
  2432.  
  2433. _VOID_
  2434. unrequest_sigio ()
  2435. {
  2436.   sighold (SIGTINT);
  2437.  
  2438.   interrupts_deferred = 1;
  2439. }
  2440.  
  2441. /* still inside #ifdef BSD4_1 */
  2442. #ifdef subprocesses
  2443.  
  2444. int sigheld; /* Mask of held signals */
  2445.  
  2446. _VOID_
  2447. sigholdx (signum)
  2448.      int signum;
  2449. {
  2450.   sigheld |= sigbit (signum);
  2451.   sighold (signum);
  2452. }
  2453.  
  2454. _VOID_
  2455. sigisheld (signum)
  2456.      int signum;
  2457. {
  2458.   sigheld |= sigbit (signum);
  2459. }
  2460.  
  2461. _VOID_
  2462. sigunhold (signum)
  2463.      int signum;
  2464. {
  2465.   sigheld &= ~sigbit (signum);
  2466.   sigrelse (signum);
  2467. }
  2468.  
  2469. _VOID_
  2470. sigfree ()    /* Free all held signals */
  2471. {
  2472.   int i;
  2473.   for (i = 0; i < NSIG; i++)
  2474.     if (sigheld & sigbit (i))
  2475.       sigrelse (i);
  2476.   sigheld = 0;
  2477. }
  2478.  
  2479. _VOID_
  2480. sigbit (i)
  2481.     int i;
  2482. {
  2483.   return 1 << (i - 1);
  2484. }
  2485. #endif /* subprocesses */
  2486. #endif /* BSD4_1 */
  2487.  
  2488. /* POSIX signals support - DJB */
  2489. /* Anyone with POSIX signals should have ANSI C declarations */
  2490.  
  2491. #ifdef POSIX_SIGNALS
  2492.  
  2493. sigset_t old_mask, empty_mask, full_mask, temp_mask;
  2494. static struct sigaction new_action, old_action;
  2495.  
  2496. _VOID_
  2497. init_signals ()
  2498. {
  2499.   sigemptyset (&empty_mask);
  2500.   sigfillset (&full_mask);
  2501. }
  2502.  
  2503. signal_handler_t
  2504. sys_signal (int signal_number, signal_handler_t action)
  2505. {
  2506. #ifdef DGUX
  2507.   /* This gets us restartable system calls for efficiency.
  2508.      The "else" code will works as well. */
  2509.   return (berk_signal (signal_number, action));
  2510. #else
  2511.   sigemptyset (&new_action.sa_mask);
  2512.   new_action.sa_handler = action;
  2513.   new_action.sa_flags = 0;
  2514.   sigaction (signal_number, &new_action, &old_action);
  2515.   return (old_action.sa_handler);
  2516. #endif /* DGUX */
  2517. }
  2518.  
  2519. #ifndef __GNUC__
  2520. /* If we're compiling with GCC, we don't need this function, since it
  2521.    can be written as a macro.  */
  2522. sigset_t
  2523. sys_sigmask (int sig)
  2524. {
  2525.   sigset_t mask;
  2526.   sigemptyset (&mask);
  2527.   sigaddset (&mask, sig);
  2528.   return mask;
  2529. }
  2530. #endif
  2531.  
  2532. int
  2533. sys_sigpause (sigset_t new_mask)
  2534. {
  2535.   /* pause emulating berk sigpause... */
  2536.   sigsuspend (&new_mask);
  2537.   return (EINTR);
  2538. }
  2539.  
  2540. /* I'd like to have these guys return pointers to the mask storage in here,
  2541.    but there'd be trouble if the code was saving multiple masks.  I'll be
  2542.    safe and pass the structure.  It normally won't be more than 2 bytes
  2543.    anyhow. - DJB */
  2544.  
  2545. sigset_t
  2546. sys_sigblock (sigset_t new_mask)
  2547. {
  2548.   sigset_t old_mask;
  2549.   sigprocmask (SIG_BLOCK, &new_mask, &old_mask);
  2550.   return (old_mask);
  2551. }
  2552.  
  2553. sigset_t
  2554. sys_sigunblock (sigset_t new_mask)
  2555. {
  2556.   sigset_t old_mask;
  2557.   sigprocmask (SIG_UNBLOCK, &new_mask, &old_mask);
  2558.   return (old_mask);
  2559. }
  2560.  
  2561. sigset_t
  2562. sys_sigsetmask (sigset_t new_mask)
  2563. {
  2564.   sigset_t old_mask;
  2565.   sigprocmask (SIG_SETMASK, &new_mask, &old_mask);
  2566.   return (old_mask);
  2567. }
  2568.  
  2569. #endif /* POSIX_SIGNALS */
  2570.  
  2571. #ifndef BSTRING
  2572.  
  2573. void
  2574. bzero (b, length)
  2575.      register char *b;
  2576.      register int length;
  2577. {
  2578. #ifdef VMS
  2579.   short zero = 0;
  2580.   long max_str = 65535;
  2581.  
  2582.   while (length > max_str) {
  2583.     (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
  2584.     length -= max_str;
  2585.     b += max_str;
  2586.   }
  2587.   max_str = length;
  2588.   (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
  2589. #else
  2590.   while (length-- > 0)
  2591.     *b++ = 0;
  2592. #endif /* not VMS */
  2593. }
  2594.  
  2595. /* Saying `void' requires a declaration, above, where bcopy is used
  2596.    and that declaration causes pain for systems where bcopy is a macro.  */
  2597. bcopy (b1, b2, length)
  2598.      register char *b1;
  2599.      register char *b2;
  2600.      register int length;
  2601. {
  2602. #ifdef VMS
  2603.   long max_str = 65535;
  2604.  
  2605.   while (length > max_str) {
  2606.     (void) LIB$MOVC3 (&max_str, b1, b2);
  2607.     length -= max_str;
  2608.     b1 += max_str;
  2609.     b2 += max_str;
  2610.   }
  2611.   max_str = length;
  2612.   (void) LIB$MOVC3 (&length, b1, b2);
  2613. #else
  2614.   while (length-- > 0)
  2615.     *b2++ = *b1++;
  2616. #endif /* not VMS */
  2617. }
  2618.  
  2619. int
  2620. bcmp (b1, b2, length)    /* This could be a macro! */
  2621.      register char *b1;
  2622.      register char *b2;
  2623.      register int length;
  2624. {
  2625. #ifdef VMS
  2626.   struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
  2627.   struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
  2628.  
  2629.   return STR$COMPARE (&src1, &src2);
  2630. #else
  2631.   while (length-- > 0)
  2632.     if (*b1++ != *b2++)
  2633.       return 1;
  2634.  
  2635.   return 0;
  2636. #endif /* not VMS */
  2637. }
  2638. #endif /* not BSTRING */
  2639.  
  2640. #ifndef HAVE_RANDOM
  2641. #ifdef USG
  2642. /*
  2643.  *    The BSD random returns numbers in the range of
  2644.  *    0 to 2e31 - 1.  The USG rand returns numbers in the
  2645.  *    range of 0 to 2e15 - 1.  This is probably not significant
  2646.  *    in this usage.
  2647.  */
  2648.   
  2649. long
  2650. random ()
  2651. {
  2652.   /* Arrange to return a range centered on zero.  */
  2653.   return (rand () << 15) + rand () - (1 << 29);
  2654. }
  2655.  
  2656. _VOID_
  2657. srandom (arg)
  2658.      int arg;
  2659. {
  2660.   srand (arg);
  2661. }
  2662.  
  2663. #endif /* USG */
  2664.  
  2665. #ifdef BSD4_1
  2666. long random ()
  2667. {
  2668.   /* Arrange to return a range centered on zero.  */
  2669.   return (rand () << 15) + rand () - (1 << 29);
  2670. }
  2671.  
  2672. _VOID_
  2673. srandom (arg)
  2674.      int arg;
  2675. {
  2676.   srand (arg);
  2677. }
  2678. #endif /* BSD4_1 */
  2679. #endif
  2680.  
  2681. #ifdef WRONG_NAME_INSQUE
  2682.  
  2683. _VOID_
  2684. insque (q,p)
  2685.      caddr_t q,p;
  2686. {
  2687.   _insque (q,p);
  2688. }
  2689.  
  2690. #endif
  2691.  
  2692. #ifdef VMS
  2693.  
  2694. #ifdef getenv
  2695. /* If any place else asks for the TERM variable,
  2696.    allow it to be overridden with the EMACS_TERM variable
  2697.    before attempting to translate the logical name TERM.  As a last
  2698.    resort, ask for VAX C's special idea of the TERM variable.  */
  2699. #undef getenv
  2700. char *
  2701. sys_getenv (name)
  2702.      char *name;
  2703. {
  2704.   register char *val;
  2705.   static char buf[256];
  2706.   static struct dsc$descriptor_s equiv
  2707.     = {sizeof (buf), DSC$K_DTYPE_T, DSC$K_CLASS_S, buf};
  2708.   static struct dsc$descriptor_s d_name
  2709.     = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
  2710.   short eqlen;
  2711.  
  2712.   if (!strcmp (name, "TERM"))
  2713.     {
  2714.       val = (char *) getenv ("EMACS_TERM");
  2715.       if (val)
  2716.     return val;
  2717.     }
  2718.  
  2719.   d_name.dsc$w_length = strlen (name);
  2720.   d_name.dsc$a_pointer = name;
  2721.   if (LIB$SYS_TRNLOG (&d_name, &eqlen, &equiv) == 1)
  2722.     {
  2723.       char *str = (char *) xmalloc (eqlen + 1);
  2724.       bcopy (buf, str, eqlen);
  2725.       str[eqlen] = '\0';
  2726.       /* This is a storage leak, but a pain to fix.  With luck,
  2727.      no one will ever notice.  */
  2728.       return str;
  2729.     }
  2730.   return (char *) getenv (name);
  2731. }
  2732. #endif /* getenv */
  2733.  
  2734. #ifdef abort
  2735. /* Since VMS doesn't believe in core dumps, the only way to debug this beast is
  2736.    to force a call on the debugger from within the image. */
  2737. #undef abort
  2738. _VOID_
  2739. sys_abort ()
  2740. {
  2741.   reset_sys_modes ();
  2742.   LIB$SIGNAL (SS$_DEBUG);
  2743. }
  2744. #endif /* abort */
  2745. #endif /* VMS */
  2746.  
  2747. #ifdef VMS
  2748. #ifdef LINK_CRTL_SHARE
  2749. #ifdef SHAREABLE_LIB_BUG
  2750. /* Variables declared noshare and initialized in sharable libraries
  2751.    cannot be shared.  The VMS linker incorrectly forces you to use a private
  2752.    version which is uninitialized... If not for this "feature", we
  2753.    could use the C library definition of sys_nerr and sys_errlist. */
  2754. int sys_nerr = 35;
  2755. char *sys_errlist[] =
  2756.   {
  2757.     "error 0",
  2758.     "not owner",
  2759.     "no such file or directory",
  2760.     "no such process",
  2761.     "interrupted system call",
  2762.     "i/o error",
  2763.     "no such device or address",
  2764.     "argument list too long",
  2765.     "exec format error",
  2766.     "bad file number",
  2767.     "no child process",
  2768.     "no more processes",
  2769.     "not enough memory",
  2770.     "permission denied",
  2771.     "bad address",
  2772.     "block device required",
  2773.     "mount devices busy",
  2774.     "file exists",
  2775.     "cross-device link",
  2776.     "no such device",
  2777.     "not a directory",
  2778.     "is a directory",
  2779.     "invalid argument",
  2780.     "file table overflow",
  2781.     "too many open files",
  2782.     "not a typewriter",
  2783.     "text file busy",
  2784.     "file too big",
  2785.     "no space left on device",
  2786.     "illegal seek",
  2787.     "read-only file system",
  2788.     "too many links",
  2789.     "broken pipe",
  2790.     "math argument",
  2791.     "result too large",
  2792.     "I/O stream empty",
  2793.     "vax/vms specific error code nontranslatable error"
  2794.   };
  2795. #endif /* SHAREABLE_LIB_BUG */
  2796. #endif /* LINK_CRTL_SHARE */
  2797. #else
  2798. #ifndef WINDOWSNT
  2799. /* Variables declared noshare and initialized in shareable libraries
  2800.    cannot be shared.  The VMS linker incorrectly forces you to use a private
  2801.    version which is uninitialized... If not for this "feature", we
  2802.    could use the C library definition of sys_nerr and sys_errlist. */
  2803. int sys_nerr = 35;
  2804. char *sys_errlist[] =
  2805.   {
  2806.     "error 0",
  2807.     "not owner",
  2808.     "no such file or directory",
  2809.     "no such process",
  2810.     "interrupted system call",
  2811.     "i/o error",
  2812.     "no such device or address",
  2813.     "argument list too long",
  2814.     "exec format error",
  2815.     "bad file number",
  2816.     "no child process",
  2817.     "no more processes",
  2818.     "not enough memory",
  2819.     "permission denied",
  2820.     "bad address",
  2821.     "block device required",
  2822.     "mount devices busy",
  2823.     "file exists",
  2824.     "cross-device link",
  2825.     "no such device",
  2826.     "not a directory",
  2827.     "is a directory",
  2828.     "invalid argument",
  2829.     "file table overflow",
  2830.     "too many open files",
  2831.     "not a typewriter",
  2832.     "text file busy",
  2833.     "file too big",
  2834.     "no space left on device",
  2835.     "illegal seek",
  2836.     "read-only file system",
  2837.     "too many links",
  2838.     "broken pipe",
  2839.     "math argument",
  2840.     "result too large",
  2841.     "I/O stream empty",
  2842.     "vax/vms specific error code nontranslatable error"
  2843.   };
  2844. #endif
  2845. #endif /* VMS */
  2846.  
  2847. #ifdef INTERRUPTIBLE_OPEN
  2848.  
  2849. /* VARARGS 2 */
  2850. int
  2851. sys_open (path, oflag, mode)
  2852.      char *path;
  2853.      int oflag, mode;
  2854. {
  2855.   register int rtnval;
  2856.   
  2857.   while ((rtnval = open (path, oflag, mode)) == -1
  2858.      && (errno == EINTR));
  2859.   return (rtnval);
  2860. }
  2861.  
  2862. #endif /* INTERRUPTIBLE_OPEN */
  2863.  
  2864. #ifdef INTERRUPTIBLE_CLOSE
  2865.  
  2866. _VOID_
  2867. sys_close (fd)
  2868.      int fd;
  2869. {
  2870.   register int rtnval;
  2871.  
  2872.   while ((rtnval = close (fd)) == -1
  2873.      && (errno == EINTR));
  2874.   return rtnval;
  2875. }
  2876.  
  2877. #endif /* INTERRUPTIBLE_CLOSE */
  2878.  
  2879. #ifdef INTERRUPTIBLE_IO
  2880.  
  2881. int
  2882. sys_read (fildes, buf, nbyte)
  2883.      int fildes;
  2884.      char *buf;
  2885.      unsigned int nbyte;
  2886. {
  2887.   register int rtnval;
  2888.   
  2889.   while ((rtnval = read (fildes, buf, nbyte)) == -1
  2890.      && (errno == EINTR));
  2891.   return (rtnval);
  2892. }
  2893.  
  2894. int
  2895. sys_write (fildes, buf, nbyte)
  2896.      int fildes;
  2897.      char *buf;
  2898.      unsigned int nbyte;
  2899. {
  2900.   register int rtnval;
  2901.  
  2902.   while ((rtnval = write (fildes, buf, nbyte)) == -1
  2903.      && (errno == EINTR));
  2904.   return (rtnval);
  2905. }
  2906.  
  2907. #endif /* INTERRUPTIBLE_IO */
  2908.  
  2909. #ifdef USG
  2910. /*
  2911.  *    All of the following are for USG.
  2912.  *
  2913.  *    On USG systems the system calls are INTERRUPTIBLE by signals
  2914.  *    that the user program has elected to catch.  Thus the system call
  2915.  *    must be retried in these cases.  To handle this without massive
  2916.  *    changes in the source code, we remap the standard system call names
  2917.  *    to names for our own functions in sysdep.c that do the system call
  2918.  *    with retries.  Actually, for portability reasons, it is good
  2919.  *    programming practice, as this example shows, to limit all actual
  2920.  *    system calls to a single occurrence in the source.  Sure, this
  2921.  *    adds an extra level of function call overhead but it is almost
  2922.  *    always negligible.   Fred Fish, Unisoft Systems Inc.
  2923.  */
  2924.  
  2925. #ifndef HAVE_SYS_SIGLIST
  2926. char *sys_siglist[NSIG + 1] =
  2927. {
  2928. #ifdef AIX
  2929. /* AIX has changed the signals a bit */
  2930.   "bogus signal",            /* 0 */
  2931.   "hangup",                /* 1  SIGHUP */
  2932.   "interrupt",                /* 2  SIGINT */
  2933.   "quit",                /* 3  SIGQUIT */
  2934.   "illegal instruction",        /* 4  SIGILL */
  2935.   "trace trap",                /* 5  SIGTRAP */
  2936.   "IOT instruction",            /* 6  SIGIOT */
  2937.   "crash likely",            /* 7  SIGDANGER */
  2938.   "floating point exception",        /* 8  SIGFPE */
  2939.   "kill",                /* 9  SIGKILL */
  2940.   "bus error",                /* 10 SIGBUS */
  2941.   "segmentation violation",        /* 11 SIGSEGV */
  2942.   "bad argument to system call",    /* 12 SIGSYS */
  2943.   "write on a pipe with no one to read it", /* 13 SIGPIPE */
  2944.   "alarm clock",            /* 14 SIGALRM */
  2945.   "software termination signum",    /* 15 SIGTERM */
  2946.   "user defined signal 1",        /* 16 SIGUSR1 */
  2947.   "user defined signal 2",        /* 17 SIGUSR2 */
  2948.   "death of a child",            /* 18 SIGCLD */
  2949.   "power-fail restart",            /* 19 SIGPWR */
  2950.   "bogus signal",            /* 20 */
  2951.   "bogus signal",            /* 21 */
  2952.   "bogus signal",            /* 22 */
  2953.   "bogus signal",            /* 23 */
  2954.   "bogus signal",            /* 24 */
  2955.   "LAN I/O interrupt",            /* 25 SIGAIO */
  2956.   "PTY I/O interrupt",            /* 26 SIGPTY */
  2957.   "I/O intervention required",        /* 27 SIGIOINT */
  2958.   "HFT grant",                /* 28 SIGGRANT */
  2959.   "HFT retract",            /* 29 SIGRETRACT */
  2960.   "HFT sound done",            /* 30 SIGSOUND */
  2961.   "HFT input ready",            /* 31 SIGMSG */
  2962. #else /* not AIX */
  2963.   "bogus signal",            /* 0 */
  2964.   "hangup",                /* 1  SIGHUP */
  2965.   "interrupt",                /* 2  SIGINT */
  2966.   "quit",                /* 3  SIGQUIT */
  2967.   "illegal instruction",        /* 4  SIGILL */
  2968.   "trace trap",                /* 5  SIGTRAP */
  2969.   "IOT instruction",            /* 6  SIGIOT */
  2970.   "EMT instruction",            /* 7  SIGEMT */
  2971.   "floating point exception",        /* 8  SIGFPE */
  2972.   "kill",                /* 9  SIGKILL */
  2973.   "bus error",                /* 10 SIGBUS */
  2974.   "segmentation violation",        /* 11 SIGSEGV */
  2975.   "bad argument to system call",    /* 12 SIGSYS */
  2976.   "write on a pipe with no one to read it", /* 13 SIGPIPE */
  2977.   "alarm clock",            /* 14 SIGALRM */
  2978.   "software termination signum",    /* 15 SIGTERM */
  2979.   "user defined signal 1",        /* 16 SIGUSR1 */
  2980.   "user defined signal 2",        /* 17 SIGUSR2 */
  2981.   "death of a child",            /* 18 SIGCLD */
  2982.   "power-fail restart",            /* 19 SIGPWR */
  2983. #endif /* not AIX */
  2984.   0
  2985.   };
  2986. #endif /* HAVE_SYS_SIGLIST */
  2987.  
  2988. /*
  2989.  *    Warning, this function may not duplicate 4.2 action properly
  2990.  *    under error conditions.
  2991.  */
  2992.  
  2993. #ifndef MAXPATHLEN
  2994. /* In 4.1, param.h fails to define this.  */
  2995. #define MAXPATHLEN 1024
  2996. #endif
  2997.  
  2998. #ifndef HAVE_GETWD
  2999.  
  3000. char *
  3001. getwd (pathname)
  3002.      char *pathname;
  3003. {
  3004.   char *npath, *spath;
  3005.   extern char *getcwd ();
  3006.  
  3007.   BLOCK_INPUT;            /* getcwd uses malloc */
  3008.   spath = npath = getcwd ((char *) 0, MAXPATHLEN);
  3009.   /* On Altos 3068, getcwd can return @hostname/dir, so discard
  3010.      up to first slash.  Should be harmless on other systems.  */
  3011.   while (*npath && *npath != '/')
  3012.     npath++;
  3013.   strcpy (pathname, npath);
  3014.   free (spath);            /* getcwd uses malloc */
  3015.   UNBLOCK_INPUT;
  3016.   return pathname;
  3017. }
  3018.  
  3019. #endif /* HAVE_GETWD */
  3020.  
  3021. /*
  3022.  *    Emulate rename using unlink/link.  Note that this is
  3023.  *    only partially correct.  Also, doesn't enforce restriction
  3024.  *    that files be of same type (regular->regular, dir->dir, etc).
  3025.  */
  3026.  
  3027. #ifndef HAVE_RENAME
  3028.  
  3029. int
  3030. rename (from, to)
  3031.      const char *from;
  3032.      const char *to;
  3033. {
  3034.   if (access (from, 0) == 0)
  3035.     {
  3036.       unlink (to);
  3037.       if (link (from, to) == 0)
  3038.     if (unlink (from) == 0)
  3039.       return (0);
  3040.     }
  3041.   return (-1);
  3042. }
  3043.  
  3044. #endif
  3045.  
  3046. #ifndef HAVE_VFORK
  3047.  
  3048. /*
  3049.  *    Substitute fork for vfork on USG flavors.
  3050.  */
  3051.  
  3052. int
  3053. vfork ()
  3054. {
  3055. #ifdef WINDOWSNT
  3056.   /* vfork is not allowed */
  3057.   return -1;
  3058. #else /* !WINDOWSNT */
  3059.   return (fork ());
  3060. #endif    /* !WINDOWSNT */
  3061. }
  3062.  
  3063. #endif /* not HAVE_VFORK */
  3064.  
  3065. #ifdef MISSING_UTIMES
  3066.  
  3067. /* HPUX (among others) sets HAVE_TIMEVAL but does not implement utimes.  */
  3068.  
  3069. utimes ()
  3070. {
  3071. }
  3072. #endif
  3073.  
  3074. #ifdef IRIS_UTIME
  3075.  
  3076. /* The IRIS (3.5) has timevals, but uses sys V utime, and doesn't have the
  3077.    utimbuf structure defined anywhere but in the man page. */
  3078.  
  3079. struct utimbuf
  3080.  {
  3081.    long actime;
  3082.    long modtime;
  3083.  };
  3084.  
  3085. utimes (name, tvp)
  3086.      char *name;
  3087.      struct timeval tvp[];
  3088. {
  3089.   struct utimbuf utb;
  3090.   utb.actime  = tvp[0].tv_sec;
  3091.   utb.modtime = tvp[1].tv_sec;
  3092.   utime (name, &utb);
  3093. }
  3094. #endif /* IRIS_UTIME */
  3095.  
  3096.  
  3097. #ifdef HPUX
  3098. #ifndef HAVE_PERROR
  3099.  
  3100. /* HPUX curses library references perror, but as far as we know
  3101.    it won't be called.  Anyway this definition will do for now.  */
  3102.  
  3103. perror ()
  3104. {
  3105. }
  3106.  
  3107. #endif /* not HAVE_PERROR */
  3108. #endif /* HPUX */
  3109.  
  3110. #ifndef HAVE_DUP2
  3111.  
  3112. /*
  3113.  *    Emulate BSD dup2.  First close newd if it already exists.
  3114.  *    Then, attempt to dup oldd.  If not successful, call dup2 recursively
  3115.  *    until we are, then close the unsuccessful ones.
  3116.  */
  3117.  
  3118. int
  3119. dup2 (oldd, newd)
  3120.      int oldd;
  3121.      int newd;
  3122. {
  3123.   register int fd, ret;
  3124.   
  3125.   sys_close (newd);
  3126.  
  3127. #ifdef F_DUPFD
  3128.   fd = fcntl (oldd, F_DUPFD, newd);
  3129.   if (fd != newd)
  3130.     error ("can't dup2 (%i,%i) : %s", oldd, newd, sys_errlist[errno]);
  3131. #else
  3132.   fd = dup (old);
  3133.   if (fd == -1)
  3134.     return -1;
  3135.   if (fd == new)
  3136.     return new;
  3137.   ret = dup2 (old,new);
  3138.   sys_close (fd);
  3139.   return ret;
  3140. #endif
  3141. }
  3142.  
  3143. #endif /* not HAVE_DUP2 */
  3144.  
  3145. /*
  3146.  *    Gettimeofday.  Simulate as much as possible.  Only accurate
  3147.  *    to nearest second.  Emacs doesn't use tzp so ignore it for now.
  3148.  *    Only needed when subprocesses are defined.
  3149.  */
  3150.  
  3151. #ifdef subprocesses
  3152. #ifndef VMS
  3153. #ifndef HAVE_GETTIMEOFDAY
  3154. #ifdef HAVE_TIMEVAL
  3155.  
  3156. /* ARGSUSED */
  3157. _VOID_
  3158. gettimeofday (tp, tzp)
  3159.      struct timeval *tp;
  3160.      struct timezone *tzp;
  3161. {
  3162.   extern long time ();
  3163.  
  3164.   tp->tv_sec = time ((long *)0);    
  3165.   tp->tv_usec = 0;
  3166.   if (tzp != 0)
  3167.     tzp->tz_minuteswest = -1;
  3168. }
  3169.  
  3170. #endif
  3171. #endif
  3172. #endif
  3173. #endif /* subprocess && !HAVE_GETTIMEOFDAY && HAVE_TIMEVAL && !VMS */
  3174.   
  3175. /*
  3176.  *    This function will go away as soon as all the stubs fixed. (fnf)
  3177.  */
  3178.  
  3179. _VOID_
  3180. croak (badfunc)
  3181.      char *badfunc;
  3182. {
  3183.   printf ("%s not yet implemented\r\n", badfunc);
  3184.   reset_sys_modes ();
  3185.   exit (1);
  3186. }
  3187.  
  3188. #endif /* USG */
  3189.  
  3190. #ifdef DGUX
  3191.  
  3192. char *sys_siglist[NSIG + 1] =
  3193. {
  3194.   "null signal",             /*  0 SIGNULL   */
  3195.   "hangup",                 /*  1 SIGHUP    */
  3196.   "interrupt",                        /*  2 SIGINT    */
  3197.   "quit",                 /*  3 SIGQUIT   */
  3198.   "illegal instruction",         /*  4 SIGILL    */
  3199.   "trace trap",                 /*  5 SIGTRAP   */
  3200.   "abort termination",             /*  6 SIGABRT   */
  3201.   "SIGEMT",                 /*  7 SIGEMT    */
  3202.   "floating point exception",         /*  8 SIGFPE    */
  3203.   "kill",                 /*  9 SIGKILL   */
  3204.   "bus error",                 /* 10 SIGBUS    */
  3205.   "segmentation violation",         /* 11 SIGSEGV   */
  3206.   "bad argument to system call",     /* 12 SIGSYS    */
  3207.   "write on a pipe with no reader",     /* 13 SIGPIPE   */
  3208.   "alarm clock",             /* 14 SIGALRM   */
  3209.   "software termination signal",     /* 15 SIGTERM   */
  3210.   "user defined signal 1",         /* 16 SIGUSR1   */
  3211.   "user defined signal 2",         /* 17 SIGUSR2   */
  3212.   "child stopped or terminated",     /* 18 SIGCLD    */
  3213.   "power-fail restart",             /* 19 SIGPWR    */
  3214.   "window size changed",         /* 20 SIGWINCH  */
  3215.   "undefined",                 /* 21           */
  3216.   "pollable event occurred",         /* 22 SIGPOLL   */
  3217.   "sendable stop signal not from tty",     /* 23 SIGSTOP   */
  3218.   "stop signal from tty",         /* 24 SIGSTP    */
  3219.   "continue a stopped process",         /* 25 SIGCONT   */
  3220.   "attempted background tty read",     /* 26 SIGTTIN   */
  3221.   "attempted background tty write",     /* 27 SIGTTOU   */
  3222.   "undefined",                 /* 28           */
  3223.   "undefined",                 /* 29           */
  3224.   "undefined",                 /* 30           */
  3225.   "undefined",                 /* 31           */
  3226.   "undefined",                 /* 32           */
  3227.   "socket (TCP/IP) urgent data arrival", /* 33 SIGURG    */
  3228.   "I/O is possible",             /* 34 SIGIO     */
  3229.   "exceeded cpu time limit",         /* 35 SIGXCPU   */
  3230.   "exceeded file size limit",         /* 36 SIGXFSZ   */
  3231.   "virtual time alarm",             /* 37 SIGVTALRM */
  3232.   "profiling time alarm",         /* 38 SIGPROF   */
  3233.   "undefined",                 /* 39           */
  3234.   "file record locks revoked",         /* 40 SIGLOST   */
  3235.   "undefined",                 /* 41           */
  3236.   "undefined",                 /* 42           */
  3237.   "undefined",                 /* 43           */
  3238.   "undefined",                 /* 44           */
  3239.   "undefined",                 /* 45           */
  3240.   "undefined",                 /* 46           */
  3241.   "undefined",                 /* 47           */
  3242.   "undefined",                 /* 48           */
  3243.   "undefined",                 /* 49           */
  3244.   "undefined",                 /* 50           */
  3245.   "undefined",                 /* 51           */
  3246.   "undefined",                 /* 52           */
  3247.   "undefined",                 /* 53           */
  3248.   "undefined",                 /* 54           */
  3249.   "undefined",                 /* 55           */
  3250.   "undefined",                 /* 56           */
  3251.   "undefined",                 /* 57           */
  3252.   "undefined",                 /* 58           */
  3253.   "undefined",                 /* 59           */
  3254.   "undefined",                 /* 60           */
  3255.   "undefined",                 /* 61           */
  3256.   "undefined",                 /* 62           */
  3257.   "undefined",                 /* 63           */
  3258.   "notification message in mess. queue", /* 64 SIGDGNOTIFY */
  3259.   0
  3260. };
  3261.  
  3262. #endif /* DGUX */
  3263.  
  3264. /* Directory routines for systems that don't have them. */
  3265.  
  3266. #ifdef SYSV_SYSTEM_DIR
  3267.  
  3268. #include <dirent.h>
  3269.  
  3270. #ifndef HAVE_CLOSEDIR
  3271. int
  3272. closedir (dirp)
  3273.      register DIR *dirp;              /* stream from opendir */
  3274. {
  3275.   sys_close (dirp->dd_fd);
  3276.  
  3277.   /* Some systems (like Solaris) allocate the buffer and the DIR all
  3278.      in one block.  Why in the world are we freeing this ourselves
  3279.      anyway?  */
  3280. #if ! (defined (sun) && defined (USG5_4))
  3281.   xfree ((char *) dirp->dd_buf); /* directory block defined in <dirent.h> */
  3282. #endif
  3283.   xfree ((char *) dirp);
  3284. }
  3285. #endif /* not HAVE_CLOSEDIR */
  3286. #endif /* SYSV_SYSTEM_DIR */
  3287.  
  3288. #ifdef NONSYSTEM_DIR_LIBRARY
  3289.  
  3290. #ifdef    WINDOWSNT
  3291. static    int    Finding = 0;
  3292. static    HANDLE    FindHandle;
  3293. #endif    /* !WINDOWSNT */
  3294.  
  3295. DIR *
  3296. opendir (filename)
  3297.      char *filename;    /* name of directory */
  3298. {
  3299.   register DIR *dirp;        /* -> malloc'ed storage */
  3300. #ifdef    WINDOWSNT
  3301.     /*
  3302.      * Open in is done by FindFirstFile, however a read is inherent to
  3303.      * this operation, so we have a flag and handle the open at read
  3304.      * time.  This flag essentially means "there is a find-handle open and
  3305.      * it needs to be closed.
  3306.      */
  3307.  
  3308.     if (!(dirp = (DIR *)malloc(sizeof (DIR)))) {
  3309.         return 0;
  3310.     }
  3311.  
  3312.     /* I hope zippy doesn't check these fields anywhere... */
  3313.  
  3314.     dirp -> dd_fd = 0;
  3315.     dirp -> dd_loc = 0;
  3316.     dirp -> dd_size = 0;
  3317.  
  3318.     /*
  3319.      * This is tacky, but we need the directory name for our
  3320.      * implementation of readdir()
  3321.      */
  3322.  
  3323.     strncpy(dirp -> dd_buf, filename, DIRBLKSIZ);
  3324. #else /* !WINDOWSNT */
  3325.   register int fd;        /* file descriptor for read */
  3326.   struct stat sbuf;        /* result of fstat */
  3327.  
  3328.   fd = sys_open (filename, 0);
  3329.   if (fd < 0)
  3330.     return 0;
  3331.  
  3332.   BLOCK_INPUT;
  3333.   if (fstat (fd, &sbuf) < 0
  3334.       || (sbuf.st_mode & S_IFMT) != S_IFDIR
  3335.       || (dirp = (DIR *) malloc (sizeof (DIR))) == 0)
  3336.     {
  3337.       sys_close (fd);
  3338.       UNBLOCK_INPUT;
  3339.       return 0;        /* bad luck today */
  3340.     }
  3341.   UNBLOCK_INPUT;
  3342.  
  3343.   dirp->dd_fd = fd;
  3344.   dirp->dd_loc = dirp->dd_size = 0;    /* refill needed */
  3345.  
  3346. #endif /* !WINDOWSNT */
  3347.   return dirp;
  3348. }
  3349.  
  3350. void
  3351. closedir (dirp)
  3352.      register DIR *dirp;        /* stream from opendir */
  3353. {
  3354. #ifdef    WINDOWSNT
  3355.     /*
  3356.      * If we have a find-handle open the we need to close it
  3357.      */
  3358.     if (Finding) {
  3359.         FindClose(FindHandle);
  3360.         Finding = 0;
  3361.     }
  3362. #else /* !WINDOWSNT */
  3363.   sys_close (dirp->dd_fd);
  3364. #endif /* !WINDOWSNT */
  3365.   xfree ((char *) dirp);
  3366. }
  3367.  
  3368.  
  3369. #ifndef VMS
  3370. #define DIRSIZ    14
  3371. struct olddir
  3372.   {
  3373.     ino_t od_ino;         /* inode */
  3374.     char od_name[DIRSIZ];    /* filename */
  3375.   };
  3376. #endif /* not VMS */
  3377.  
  3378. struct direct dir_static;    /* simulated directory contents */
  3379.  
  3380. /* ARGUSED */
  3381. struct direct *
  3382. readdir (dirp)
  3383.      register DIR *dirp;    /* stream from opendir */
  3384. {
  3385. #ifdef WINDOWSNT
  3386.     WIN32_FIND_DATA    FindData;
  3387.  
  3388.     /* If we aren't Finding, do a find-first, otherwise do a find-next. */
  3389.  
  3390.     if (!Finding) {
  3391.         char    filename[MAXNAMLEN + 3];
  3392.                 int ln;
  3393.  
  3394.         strncpy(filename, dirp -> dd_buf, MAXNAMLEN);
  3395.                 ln = strlen(filename)-1;
  3396.                 if (filename[ln] != '\\' && filename[ln] != ':')
  3397.                     strcat(filename, "\\");
  3398.         strcat(filename, "*.*");
  3399.  
  3400.         FindHandle = FindFirstFile(filename, &FindData);
  3401.  
  3402.         if (FindHandle == INVALID_HANDLE_VALUE) {
  3403.             return 0;
  3404.         }
  3405.  
  3406.         Finding = 1;
  3407.     } else {
  3408.         if (!FindNextFile(FindHandle, &FindData)) {
  3409.             return 0;
  3410.         }
  3411.     }
  3412.  
  3413.         /* Don't return . or .. since it doesn't look like any of the
  3414.            readdir calling code expects them */
  3415.         while (strcmp(FindData.cFileName, ".") == 0 ||
  3416.                strcmp(FindData.cFileName, "..") == 0)
  3417.         {
  3418.             if (!FindNextFile(FindHandle, &FindData))
  3419.                 return 0;
  3420.         }
  3421.         
  3422.     /*
  3423.      * There is not such thing as I-node number under Windows NT(tm), but
  3424.      * evidently a number of 0 means it's deleted...  We'll never see a
  3425.      * "deleted" file returned from Find(First,Next)File...I hope...
  3426.      */
  3427.     dir_static.d_ino = 1;
  3428.  
  3429.     dir_static.d_reclen = sizeof (struct direct) - MAXNAMLEN + 3 +
  3430.         dir_static.d_namlen - dir_static.d_namlen % 4;
  3431.  
  3432.     dir_static.d_namlen = strlen(FindData.cFileName);
  3433.     strncpy(dir_static.d_name, FindData.cFileName, MAXNAMLEN);
  3434.  
  3435.     return &dir_static;
  3436. #else /* !WINDOWSNT */
  3437. #ifndef VMS
  3438.   register struct olddir *dp;    /* -> directory data */
  3439. #else /* VMS */
  3440.   register struct dir$_name *dp; /* -> directory data */
  3441.   register struct dir$_version *dv; /* -> version data */
  3442. #endif /* VMS */
  3443.  
  3444.   for (; ;)
  3445.     {
  3446.       if (dirp->dd_loc >= dirp->dd_size)
  3447.     dirp->dd_loc = dirp->dd_size = 0;
  3448.  
  3449.       if (dirp->dd_size == 0     /* refill buffer */
  3450.       && (dirp->dd_size = sys_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
  3451.     return 0;
  3452.  
  3453. #ifndef VMS
  3454.       dp = (struct olddir *) &dirp->dd_buf[dirp->dd_loc];
  3455.       dirp->dd_loc += sizeof (struct olddir);
  3456.  
  3457.       if (dp->od_ino != 0)    /* not deleted entry */
  3458.     {
  3459.       dir_static.d_ino = dp->od_ino;
  3460.       strncpy (dir_static.d_name, dp->od_name, DIRSIZ);
  3461.       dir_static.d_name[DIRSIZ] = '\0';
  3462.       dir_static.d_namlen = strlen (dir_static.d_name);
  3463.       dir_static.d_reclen = sizeof (struct direct)
  3464.         - MAXNAMLEN + 3
  3465.           + dir_static.d_namlen - dir_static.d_namlen % 4;
  3466.       return &dir_static;    /* -> simulated structure */
  3467.     }
  3468. #else /* VMS */
  3469.       dp = (struct dir$_name *) dirp->dd_buf;
  3470.       if (dirp->dd_loc == 0)
  3471.     dirp->dd_loc = (dp->dir$b_namecount&1) ? dp->dir$b_namecount + 1
  3472.       : dp->dir$b_namecount;
  3473.       dv = (struct dir$_version *)&dp->dir$t_name[dirp->dd_loc];
  3474.       dir_static.d_ino = dv->dir$w_fid_num;
  3475.       dir_static.d_namlen = dp->dir$b_namecount;
  3476.       dir_static.d_reclen = sizeof (struct direct)
  3477.     - MAXNAMLEN + 3
  3478.       + dir_static.d_namlen - dir_static.d_namlen % 4;
  3479.       strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
  3480.       dir_static.d_name[dir_static.d_namlen] = '\0';
  3481.       dirp->dd_loc = dirp->dd_size; /* only one record at a time */
  3482.       return &dir_static;
  3483. #endif /* VMS */
  3484.     }
  3485. #endif /* !WINDOWSNT */
  3486. }
  3487.  
  3488. #ifdef VMS
  3489. /* readdirver is just like readdir except it returns all versions of a file
  3490.    as separate entries.  */
  3491.  
  3492. /* ARGUSED */
  3493. struct direct *
  3494. readdirver (dirp)
  3495.      register DIR *dirp;    /* stream from opendir */
  3496. {
  3497.   register struct dir$_name *dp; /* -> directory data */
  3498.   register struct dir$_version *dv; /* -> version data */
  3499.  
  3500.   if (dirp->dd_loc >= dirp->dd_size - sizeof (struct dir$_name))
  3501.     dirp->dd_loc = dirp->dd_size = 0;
  3502.  
  3503.   if (dirp->dd_size == 0     /* refill buffer */
  3504.       && (dirp->dd_size = sys_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
  3505.     return 0;
  3506.  
  3507.   dp = (struct dir$_name *) dirp->dd_buf;
  3508.   if (dirp->dd_loc == 0)
  3509.     dirp->dd_loc = (dp->dir$b_namecount & 1) ? dp->dir$b_namecount + 1
  3510.            : dp->dir$b_namecount;
  3511.   dv = (struct dir$_version *) &dp->dir$t_name[dirp->dd_loc];
  3512.   strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
  3513.   sprintf (&dir_static.d_name[dp->dir$b_namecount], ";%d", dv->dir$w_version);
  3514.   dir_static.d_namlen = strlen (dir_static.d_name);
  3515.   dir_static.d_ino = dv->dir$w_fid_num;
  3516.   dir_static.d_reclen = sizeof (struct direct) - MAXNAMLEN + 3
  3517.             + dir_static.d_namlen - dir_static.d_namlen % 4;
  3518.   dirp->dd_loc = ((char *) (++dv) - dp->dir$t_name);
  3519.   return &dir_static;
  3520. }
  3521.  
  3522. #endif /* VMS */
  3523.  
  3524. #endif /* NONSYSTEM_DIR_LIBRARY */
  3525.  
  3526. /* Functions for VMS */
  3527. #ifdef VMS
  3528. #include "vms-pwd.h"
  3529. #include <acldef.h>
  3530. #include <chpdef.h>
  3531. #include <jpidef.h>
  3532.  
  3533. /* Return as a string the VMS error string pertaining to STATUS.
  3534.    Reuses the same static buffer each time it is called.  */
  3535.  
  3536. char *
  3537. vmserrstr (status)
  3538.      int status;        /* VMS status code */
  3539. {
  3540.   int bufadr[2];
  3541.   short len;
  3542.   static char buf[257];
  3543.  
  3544.   bufadr[0] = sizeof buf - 1;
  3545.   bufadr[1] = (int) buf;
  3546.   if (! (SYS$GETMSG (status, &len, bufadr, 0x1, 0) & 1))
  3547.     return "untranslatable VMS error status";
  3548.   buf[len] = '\0';
  3549.   return buf;
  3550. }
  3551.  
  3552. #ifdef access
  3553. #undef access
  3554.   
  3555. /* The following is necessary because 'access' emulation by VMS C (2.0) does
  3556.  * not work correctly.  (It also doesn't work well in version 2.3.)
  3557.  */
  3558.  
  3559. #ifdef VMS4_4
  3560.  
  3561. #define DESCRIPTOR(name,string) struct dsc$descriptor_s name = \
  3562.     { strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string }
  3563.  
  3564. typedef union {
  3565.     struct {
  3566.     unsigned short s_buflen;
  3567.     unsigned short s_code;
  3568.     char *s_bufadr;
  3569.     unsigned short *s_retlenadr;
  3570.     } s;
  3571.     int end;
  3572. } item;
  3573. #define buflen s.s_buflen
  3574. #define code s.s_code
  3575. #define bufadr s.s_bufadr
  3576. #define retlenadr s.s_retlenadr
  3577.  
  3578. #define R_OK 4    /* test for read permission */
  3579. #define W_OK 2    /* test for write permission */
  3580. #define X_OK 1    /* test for execute (search) permission */
  3581. #define F_OK 0    /* test for presence of file */
  3582.  
  3583. int
  3584. sys_access (path, mode)
  3585.      char *path;
  3586.      int mode;
  3587. {
  3588.   static char *user = NULL;
  3589.   char dir_fn[512];
  3590.  
  3591.   /* translate possible directory spec into .DIR file name, so brain-dead
  3592.    * access can treat the directory like a file.  */
  3593.   if (directory_file_name (path, dir_fn))
  3594.     path = dir_fn;
  3595.   
  3596.   if (mode == F_OK)
  3597.     return access (path, mode);
  3598.   if (user == NULL && (user = (char *) getenv ("USER")) == NULL)
  3599.     return -1;
  3600.   {
  3601.     int stat;
  3602.     int flags;
  3603.     int acces;
  3604.     unsigned short int dummy;
  3605.     item itemlst[3];
  3606.     static int constant = ACL$C_FILE;
  3607.     DESCRIPTOR (path_desc, path);
  3608.     DESCRIPTOR (user_desc, user);
  3609.  
  3610.     flags = 0;
  3611.     acces = 0;
  3612.     if ((mode & X_OK) && ((stat = access (path, mode)) < 0 || mode == X_OK))
  3613.       return stat;
  3614.     if (mode & R_OK)
  3615.       acces |= CHP$M_READ;
  3616.     if (mode & W_OK)
  3617.       acces |= CHP$M_WRITE;
  3618.     itemlst[0].buflen = sizeof (int);
  3619.     itemlst[0].code = CHP$_FLAGS;
  3620.     itemlst[0].bufadr = (char *) &flags;
  3621.     itemlst[0].retlenadr = &dummy;
  3622.     itemlst[1].buflen = sizeof (int);
  3623.     itemlst[1].code = CHP$_ACCESS;
  3624.     itemlst[1].bufadr = (char *) &acces;
  3625.     itemlst[1].retlenadr = &dummy;
  3626.     itemlst[2].end = CHP$_END;
  3627.     stat = SYS$CHECK_ACCESS (&constant, &path_desc, &user_desc, itemlst);
  3628.     return stat == SS$_NORMAL ? 0 : -1;
  3629.   }
  3630. }
  3631.  
  3632. #else /* not VMS4_4 */
  3633.  
  3634. #include <prvdef.h>
  3635. #define    ACE$M_WRITE    2
  3636. #define    ACE$C_KEYID    1
  3637.  
  3638. static unsigned short memid, grpid;
  3639. static unsigned int uic;
  3640.  
  3641. /* Called from init_sys_modes, so it happens not very often
  3642.    but at least each time Emacs is loaded.  */
  3643. _VOID_
  3644. sys_access_reinit ()
  3645. {
  3646.   uic = 0;
  3647. }
  3648.  
  3649. int
  3650. sys_access (filename, type)
  3651.      char * filename;
  3652.      int type;
  3653. {
  3654.   struct FAB fab;
  3655.   struct XABPRO xab;
  3656.   int status, size, i, typecode, acl_controlled;
  3657.   unsigned int *aclptr, *aclend, aclbuf[60];
  3658.   union prvdef prvmask;
  3659.  
  3660.   /* Get UIC and GRP values for protection checking.  */
  3661.   if (uic == 0)
  3662.     {
  3663.       status = LIB$GETJPI (&JPI$_UIC, 0, 0, &uic, 0, 0);
  3664.       if (! (status & 1))
  3665.     return -1;
  3666.       memid = uic & 0xFFFF;
  3667.       grpid = uic >> 16;
  3668.     }
  3669.  
  3670.   if (type != 2)        /* not checking write access */
  3671.     return access (filename, type);
  3672.  
  3673.   /* Check write protection. */
  3674.     
  3675. #define    CHECKPRIV(bit)    (prvmask.bit)
  3676. #define    WRITEABLE(field)  (! ((xab.xab$w_pro >> field) & XAB$M_NOWRITE))
  3677.  
  3678.   /* Find privilege bits */
  3679.   status = SYS$SETPRV (0, 0, 0, prvmask);
  3680.   if (! (status & 1))
  3681.     error ("Unable to find privileges: %s", vmserrstr (status));
  3682.   if (CHECKPRIV (PRV$V_BYPASS))
  3683.     return 0;            /* BYPASS enabled */
  3684.   fab = cc$rms_fab;
  3685.   fab.fab$b_fac = FAB$M_GET;
  3686.   fab.fab$l_fna = filename;
  3687.   fab.fab$b_fns = strlen (filename);
  3688.   fab.fab$l_xab = &xab;
  3689.   xab = cc$rms_xabpro;
  3690.   xab.xab$l_aclbuf = aclbuf;
  3691.   xab.xab$w_aclsiz = sizeof (aclbuf);
  3692.   status = SYS$OPEN (&fab, 0, 0);
  3693.   if (! (status & 1))
  3694.     return -1;
  3695.   SYS$CLOSE (&fab, 0, 0);
  3696.   /* Check system access */
  3697.   if (CHECKPRIV (PRV$V_SYSPRV) && WRITEABLE (XAB$V_SYS))
  3698.     return 0;
  3699.   /* Check ACL entries, if any */
  3700.   acl_controlled = 0;
  3701.   if (xab.xab$w_acllen > 0)
  3702.     {
  3703.       aclptr = aclbuf;
  3704.       aclend = &aclbuf[xab.xab$w_acllen / 4];
  3705.       while (*aclptr && aclptr < aclend)
  3706.     {
  3707.       size = (*aclptr & 0xff) / 4;
  3708.       typecode = (*aclptr >> 8) & 0xff;
  3709.       if (typecode == ACE$C_KEYID)
  3710.         for (i = size - 1; i > 1; i--)
  3711.           if (aclptr[i] == uic)
  3712.         {
  3713.           acl_controlled = 1;
  3714.           if (aclptr[1] & ACE$M_WRITE)
  3715.             return 0;    /* Write access through ACL */
  3716.         }
  3717.       aclptr = &aclptr[size];
  3718.     }
  3719.       if (acl_controlled)    /* ACL specified, prohibits write access */
  3720.     return -1;
  3721.     }
  3722.   /* No ACL entries specified, check normal protection */
  3723.   if (WRITEABLE (XAB$V_WLD))    /* World writeable */
  3724.     return 0;
  3725.   if (WRITEABLE (XAB$V_GRP) &&
  3726.       (unsigned short) (xab.xab$l_uic >> 16) == grpid)
  3727.     return 0;            /* Group writeable */
  3728.   if (WRITEABLE (XAB$V_OWN) &&
  3729.       (xab.xab$l_uic & 0xFFFF) == memid)
  3730.     return 0;            /* Owner writeable */
  3731.  
  3732.   return -1;    /* Not writeable */
  3733. }
  3734. #endif /* not VMS4_4 */
  3735. #endif /* access */
  3736.   
  3737. static char vtbuf[NAM$C_MAXRSS+1];
  3738.  
  3739. /* translate a vms file spec to a unix path */
  3740. char *
  3741. sys_translate_vms (vfile)
  3742.      char * vfile;
  3743. {
  3744.   char * p;
  3745.   char * targ;
  3746.  
  3747.   if (!vfile)
  3748.     return 0;
  3749.  
  3750.   targ = vtbuf;
  3751.  
  3752.   /* leading device or logical name is a root directory */
  3753.   if (p = strchr (vfile, ':'))
  3754.     {
  3755.       *targ++ = '/';
  3756.       while (vfile < p)
  3757.     *targ++ = *vfile++;
  3758.       vfile++;
  3759.       *targ++ = '/';
  3760.     }
  3761.   p = vfile;
  3762.   if (*p == '[' || *p == '<')
  3763.     {
  3764.       while (*++vfile != *p + 2)
  3765.     switch (*vfile)
  3766.       {
  3767.       case '.':
  3768.         if (vfile[-1] == *p)
  3769.           *targ++ = '.';
  3770.         *targ++ = '/';
  3771.         break;
  3772.  
  3773.       case '-':
  3774.         *targ++ = '.';
  3775.         *targ++ = '.';
  3776.         break;
  3777.         
  3778.       default:
  3779.         *targ++ = *vfile;
  3780.         break;
  3781.       }
  3782.       vfile++;
  3783.       *targ++ = '/';
  3784.     }
  3785.   while (*vfile)
  3786.     *targ++ = *vfile++;
  3787.  
  3788.   return vtbuf;
  3789. }
  3790.  
  3791. static char utbuf[NAM$C_MAXRSS+1];
  3792.  
  3793. /* translate a unix path to a VMS file spec */
  3794. char *
  3795. sys_translate_unix (ufile)
  3796.      char * ufile;
  3797. {
  3798.   int slash_seen = 0;
  3799.   char *p;
  3800.   char * targ;
  3801.  
  3802.   if (!ufile)
  3803.     return 0;
  3804.  
  3805.   targ = utbuf;
  3806.  
  3807.   if (*ufile == '/')
  3808.     {
  3809.       ufile++;
  3810.     }
  3811.  
  3812.   while (*ufile)
  3813.     {
  3814.       switch (*ufile)
  3815.     {
  3816.     case '/':
  3817.       if (slash_seen)
  3818.         if (index (&ufile[1], '/'))
  3819.           *targ++ = '.';
  3820.         else
  3821.           *targ++ = ']';
  3822.       else
  3823.         {
  3824.           *targ++ = ':';
  3825.           if (index (&ufile[1], '/'))
  3826.         *targ++ = '[';
  3827.           slash_seen = 1;
  3828.         }
  3829.       break;
  3830.  
  3831.     case '.':
  3832.       if (strncmp (ufile, "./", 2) == 0)
  3833.         {
  3834.           if (!slash_seen)
  3835.         {
  3836.           *targ++ = '[';
  3837.           slash_seen = 1;
  3838.         }
  3839.           ufile++;        /* skip the dot */
  3840.           if (index (&ufile[1], '/'))
  3841.         *targ++ = '.';
  3842.           else
  3843.         *targ++ = ']';
  3844.         }
  3845.       else if (strncmp (ufile, "../", 3) == 0)
  3846.         {
  3847.           if (!slash_seen)
  3848.         {
  3849.           *targ++ = '[';
  3850.           slash_seen = 1;
  3851.         }
  3852.           *targ++ = '-';
  3853.           ufile += 2;    /* skip the dots */
  3854.           if (index (&ufile[1], '/'))
  3855.         *targ++ = '.';
  3856.           else
  3857.         *targ++ = ']';
  3858.         }
  3859.       else
  3860.         *targ++ = *ufile;
  3861.       break;
  3862.  
  3863.     default:
  3864.       *targ++ = *ufile;
  3865.       break;
  3866.     }
  3867.       ufile++;
  3868.     }
  3869.   *targ = '\0';
  3870.   
  3871.   return utbuf;
  3872. }
  3873.  
  3874. char *
  3875. getwd (pathname)
  3876.      char *pathname;
  3877. {
  3878.   char *ptr;
  3879.   extern char *getcwd ();
  3880.  
  3881. #define MAXPATHLEN 1024
  3882.  
  3883.   ptr = xmalloc (MAXPATHLEN);
  3884.   getcwd (ptr, MAXPATHLEN);
  3885.   strcpy (pathname, ptr);
  3886.   xfree (ptr);
  3887.   
  3888.  return pathname;
  3889. }
  3890.  
  3891. int
  3892. getppid ()
  3893. {
  3894.   long item_code = JPI$_OWNER;
  3895.   unsigned long parent_id;
  3896.   int status;
  3897.  
  3898.   if (((status = LIB$GETJPI (&item_code, 0, 0, &parent_id)) & 1) == 0)
  3899.     {
  3900.       errno = EVMSERR;
  3901.       vaxc$errno = status;
  3902.       return -1;
  3903.     }
  3904.   return parent_id;
  3905. }
  3906.  
  3907. #undef getuid
  3908. unsigned
  3909. sys_getuid ()
  3910. {
  3911.   return (getgid () << 16) | getuid ();
  3912. }
  3913.  
  3914. int
  3915. sys_read (fildes, buf, nbyte)
  3916.      int fildes;
  3917.      char *buf;
  3918.      unsigned int nbyte;
  3919. {
  3920.   return read (fildes, buf, (nbyte < MAXIOSIZE ? nbyte : MAXIOSIZE));
  3921. }
  3922.  
  3923. #if 0
  3924. int
  3925. sys_write (fildes, buf, nbyte)
  3926.      int fildes;
  3927.      char *buf;
  3928.      unsigned int nbyte;
  3929. {
  3930.   register int nwrote, rtnval = 0;
  3931.  
  3932.   while (nbyte > MAXIOSIZE && (nwrote = write (fildes, buf, MAXIOSIZE)) > 0) {
  3933.     nbyte -= nwrote;
  3934.     buf += nwrote;
  3935.     rtnval += nwrote;
  3936.   }
  3937.   if (nwrote < 0)
  3938.     return rtnval ? rtnval : -1;
  3939.   if ((nwrote = write (fildes, buf, nbyte)) < 0)
  3940.     return rtnval ? rtnval : -1;
  3941.   return (rtnval + nwrote);
  3942. }
  3943. #endif /* 0 */
  3944.  
  3945. /*
  3946.  *    VAX/VMS VAX C RTL really loses. It insists that records
  3947.  *      end with a newline (carriage return) character, and if they
  3948.  *    don't it adds one (nice of it isn't it!)
  3949.  *
  3950.  *    Thus we do this stupidity below.
  3951.  */
  3952.  
  3953. int
  3954. sys_write (fildes, buf, nbytes)
  3955.      int fildes;
  3956.      char *buf;
  3957.      unsigned int nbytes;
  3958. {
  3959.   register char *p;
  3960.   register char *e;
  3961.   int sum = 0;
  3962.   struct stat st;
  3963.  
  3964.   fstat (fildes, &st);
  3965.   p = buf;
  3966.   while (nbytes > 0)
  3967.     {
  3968.       int len, retval;
  3969.  
  3970.       /* Handle fixed-length files with carriage control.  */
  3971.       if (st.st_fab_rfm == FAB$C_FIX
  3972.       && ((st.st_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0))
  3973.     {
  3974.       len = st.st_fab_mrs;
  3975.       retval = write (fildes, p, min (len, nbytes));
  3976.       if (retval != len)
  3977.         return -1;
  3978.       retval++;    /* This skips the implied carriage control */
  3979.     }
  3980.       else
  3981.     {
  3982.       e =  p + min (MAXIOSIZE, nbytes) - 1;
  3983.       while (*e != '\n' && e > p) e--;
  3984.       if (p == e)        /* Ok.. so here we add a newline... sigh. */
  3985.         e = p + min (MAXIOSIZE, nbytes) - 1;
  3986.       len = e + 1 - p;
  3987.       retval = write (fildes, p, len);
  3988.       if (retval != len)
  3989.         return -1;
  3990.     }
  3991.       p += retval;
  3992.       sum += retval;
  3993.       nbytes -= retval;
  3994.     }
  3995.   return sum;
  3996. }
  3997.  
  3998. /* Create file NEW copying its attributes from file OLD.  If
  3999.    OLD is 0 or does not exist, create based on the value of
  4000.    vms_stmlf_recfm. */
  4001.  
  4002. /* Protection value the file should ultimately have.
  4003.    Set by create_copy_attrs, and use by rename_sansversions.  */
  4004. static unsigned short int fab_final_pro;
  4005.  
  4006. int
  4007. creat_copy_attrs (old, new)
  4008.      char *old, *new;
  4009. {
  4010.   struct FAB fab = cc$rms_fab;
  4011.   struct XABPRO xabpro;
  4012.   char aclbuf[256];    /* Choice of size is arbitrary.  See below. */
  4013.   extern int vms_stmlf_recfm;
  4014.  
  4015.   if (old)
  4016.     {
  4017.       fab.fab$b_fac = FAB$M_GET;
  4018.       fab.fab$l_fna = old;
  4019.       fab.fab$b_fns = strlen (old);
  4020.       fab.fab$l_xab = (char *) &xabpro;
  4021.       xabpro = cc$rms_xabpro;
  4022.       xabpro.xab$l_aclbuf = aclbuf;
  4023.       xabpro.xab$w_aclsiz = sizeof aclbuf;
  4024.       /* Call $OPEN to fill in the fab & xabpro fields. */
  4025.       if (SYS$OPEN (&fab, 0, 0) & 1)
  4026.     {
  4027.       SYS$CLOSE (&fab, 0, 0);
  4028.       fab.fab$l_alq = 0;    /* zero the allocation quantity */
  4029.       if (xabpro.xab$w_acllen > 0)
  4030.         {
  4031.           if (xabpro.xab$w_acllen > sizeof aclbuf)
  4032.         /* If the acl buffer was too short, redo open with longer one.
  4033.            Wouldn't need to do this if there were some system imposed
  4034.            limit on the size of an ACL, but I can't find any such. */
  4035.         {
  4036.           xabpro.xab$l_aclbuf = (char *) alloca (xabpro.xab$w_acllen);
  4037.           xabpro.xab$w_aclsiz = xabpro.xab$w_acllen;
  4038.           if (SYS$OPEN (&fab, 0, 0) & 1)
  4039.             SYS$CLOSE (&fab, 0, 0);
  4040.           else
  4041.             old = 0;
  4042.         }
  4043.         }
  4044.       else
  4045.         xabpro.xab$l_aclbuf = 0;
  4046.     }
  4047.       else
  4048.     old = 0;
  4049.     }
  4050.   fab.fab$l_fna = new;
  4051.   fab.fab$b_fns = strlen (new);
  4052.   if (!old)
  4053.     {
  4054.       fab.fab$l_xab = 0;
  4055.       fab.fab$b_rfm = vms_stmlf_recfm ? FAB$C_STMLF : FAB$C_VAR;
  4056.       fab.fab$b_rat = FAB$M_CR;
  4057.     }
  4058.  
  4059.   /* Set the file protections such that we will be able to manipulate
  4060.      this file.  Once we are done writing and renaming it, we will set
  4061.      the protections back.  */
  4062.   if (old)
  4063.     fab_final_pro = xabpro.xab$w_pro;
  4064.   else
  4065.     SYS$SETDFPROT (0, &fab_final_pro);
  4066.   xabpro.xab$w_pro &= 0xff0f; /* set O:rewd for now. This is set back later. */
  4067.  
  4068.   /* Create the new file with either default attrs or attrs copied
  4069.      from old file. */
  4070.   if (!(SYS$CREATE (&fab, 0, 0) & 1))
  4071.     return -1;
  4072.   SYS$CLOSE (&fab, 0, 0);
  4073.   /* As this is a "replacement" for creat, return a file descriptor
  4074.      opened for writing. */
  4075.   return open (new, O_WRONLY);
  4076. }
  4077.  
  4078. #ifdef creat
  4079. #undef creat
  4080. #include <varargs.h>
  4081. #ifdef __GNUC__
  4082. #ifndef va_count
  4083. #define va_count(X) ((X) = *(((int *) &(va_alist)) - 1))
  4084. #endif
  4085. #endif
  4086.  
  4087. sys_creat (va_alist)
  4088.      va_dcl
  4089. {
  4090.   va_list list_incrementer;
  4091.   char *name;
  4092.   int mode;
  4093.   int rfd;            /* related file descriptor */
  4094.   int fd;            /* Our new file descriptor */
  4095.   int count;
  4096.   struct stat st_buf;
  4097.   char rfm[12];
  4098.   char rat[15];
  4099.   char mrs[13];
  4100.   char fsz[13];
  4101.   extern int vms_stmlf_recfm;
  4102.  
  4103.   va_count (count);
  4104.   va_start (list_incrementer);
  4105.   name = va_arg (list_incrementer, char *);
  4106.   mode = va_arg (list_incrementer, int);
  4107.   if (count > 2)
  4108.     rfd = va_arg (list_incrementer, int);
  4109.   va_end (list_incrementer);
  4110.   if (count > 2)
  4111.     {
  4112.       /* Use information from the related file descriptor to set record
  4113.      format of the newly created file. */
  4114.       fstat (rfd, &st_buf);
  4115.       switch (st_buf.st_fab_rfm)
  4116.     {
  4117.     case FAB$C_FIX:
  4118.       strcpy (rfm, "rfm = fix");
  4119.       sprintf (mrs, "mrs = %d", st_buf.st_fab_mrs);
  4120.       strcpy (rat, "rat = ");
  4121.       if (st_buf.st_fab_rat & FAB$M_CR)
  4122.         strcat (rat, "cr");
  4123.       else if (st_buf.st_fab_rat & FAB$M_FTN)
  4124.         strcat (rat, "ftn");
  4125.       else if (st_buf.st_fab_rat & FAB$M_PRN)
  4126.         strcat (rat, "prn");
  4127.       if (st_buf.st_fab_rat & FAB$M_BLK)
  4128.         if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
  4129.           strcat (rat, ", blk");
  4130.         else
  4131.           strcat (rat, "blk");
  4132.       return creat (name, 0, rfm, rat, mrs);
  4133.  
  4134.     case FAB$C_VFC:
  4135.       strcpy (rfm, "rfm = vfc");
  4136.       sprintf (fsz, "fsz = %d", st_buf.st_fab_fsz);
  4137.       strcpy (rat, "rat = ");
  4138.       if (st_buf.st_fab_rat & FAB$M_CR)
  4139.         strcat (rat, "cr");
  4140.       else if (st_buf.st_fab_rat & FAB$M_FTN)
  4141.         strcat (rat, "ftn");
  4142.       else if (st_buf.st_fab_rat & FAB$M_PRN)
  4143.         strcat (rat, "prn");
  4144.       if (st_buf.st_fab_rat & FAB$M_BLK)
  4145.         if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
  4146.           strcat (rat, ", blk");
  4147.         else
  4148.           strcat (rat, "blk");
  4149.       return creat (name, 0, rfm, rat, fsz);
  4150.  
  4151.     case FAB$C_STM:
  4152.       strcpy (rfm, "rfm = stm");
  4153.       break;
  4154.  
  4155.     case FAB$C_STMCR:
  4156.       strcpy (rfm, "rfm = stmcr");
  4157.       break;
  4158.  
  4159.     case FAB$C_STMLF:
  4160.       strcpy (rfm, "rfm = stmlf");
  4161.       break;
  4162.  
  4163.     case FAB$C_UDF:
  4164.       strcpy (rfm, "rfm = udf");
  4165.       break;
  4166.  
  4167.     case FAB$C_VAR:
  4168.       strcpy (rfm, "rfm = var");
  4169.       break;
  4170.     }
  4171.       strcpy (rat, "rat = ");
  4172.       if (st_buf.st_fab_rat & FAB$M_CR)
  4173.     strcat (rat, "cr");
  4174.       else if (st_buf.st_fab_rat & FAB$M_FTN)
  4175.     strcat (rat, "ftn");
  4176.       else if (st_buf.st_fab_rat & FAB$M_PRN)
  4177.     strcat (rat, "prn");
  4178.       if (st_buf.st_fab_rat & FAB$M_BLK)
  4179.     if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
  4180.       strcat (rat, ", blk");
  4181.     else
  4182.       strcat (rat, "blk");
  4183.     }
  4184.   else
  4185.     {
  4186.       strcpy (rfm, vms_stmlf_recfm ? "rfm = stmlf" : "rfm=var");
  4187.       strcpy (rat, "rat=cr");
  4188.     }
  4189.   /* Until the VAX C RTL fixes the many bugs with modes, always use
  4190.      mode 0 to get the user's default protection. */
  4191.   fd = creat (name, 0, rfm, rat);
  4192.   if (fd < 0 && errno == EEXIST)
  4193.     {
  4194.       if (unlink (name) < 0)
  4195.     report_file_error ("delete", build_string (name));
  4196.       fd = creat (name, 0, rfm, rat);
  4197.     }
  4198.   return fd;
  4199. }
  4200. #endif /* creat */
  4201.  
  4202. /* fwrite to stdout is S L O W.  Speed it up by using fputc...*/
  4203. sys_fwrite (ptr, size, num, fp)
  4204.      register char * ptr;
  4205.      FILE * fp;
  4206. {
  4207.   register int tot = num * size;
  4208.  
  4209.   while (tot--)
  4210.     fputc (*ptr++, fp);
  4211. }
  4212.  
  4213. /*
  4214.  * The VMS C library routine creat actually creates a new version of an
  4215.  * existing file rather than truncating the old version.  There are times
  4216.  * when this is not the desired behavior, for instance, when writing an
  4217.  * auto save file (you only want one version), or when you don't have
  4218.  * write permission in the directory containing the file (but the file
  4219.  * itself is writable).  Hence this routine, which is equivalent to 
  4220.  * "close (creat (fn, 0));" on Unix if fn already exists.
  4221.  */
  4222. int
  4223. vms_truncate (fn)
  4224.      char *fn;
  4225. {
  4226.   struct FAB xfab = cc$rms_fab;
  4227.   struct RAB xrab = cc$rms_rab;
  4228.   int status;
  4229.  
  4230.   xfab.fab$l_fop = FAB$M_TEF;    /* free allocated but unused blocks on close */
  4231.   xfab.fab$b_fac = FAB$M_TRN | FAB$M_GET; /* allow truncate and get access */
  4232.   xfab.fab$b_shr = FAB$M_NIL;    /* allow no sharing - file must be locked */
  4233.   xfab.fab$l_fna = fn;
  4234.   xfab.fab$b_fns = strlen (fn);
  4235.   xfab.fab$l_dna = ";0";    /* default to latest version of the file */
  4236.   xfab.fab$b_dns = 2;
  4237.   xrab.rab$l_fab = &xfab;
  4238.  
  4239.   /* This gibberish opens the file, positions to the first record, and
  4240.      deletes all records from there until the end of file. */
  4241.   if ((SYS$OPEN (&xfab) & 01) == 01)
  4242.     {
  4243.       if ((SYS$CONNECT (&xrab) & 01) == 01 &&
  4244.       (SYS$FIND (&xrab) & 01) == 01 &&
  4245.       (SYS$TRUNCATE (&xrab) & 01) == 01)
  4246.     status = 0;
  4247.       else
  4248.     status = -1;
  4249.     }
  4250.   else
  4251.     status = -1;
  4252.   SYS$CLOSE (&xfab);
  4253.   return status;
  4254. }
  4255.  
  4256. /* Define this symbol to actually read SYSUAF.DAT.  This requires either
  4257.    SYSPRV or a readable SYSUAF.DAT. */
  4258.  
  4259. #ifdef READ_SYSUAF
  4260. /*
  4261.  * getuaf.c
  4262.  *
  4263.  * Routine to read the VMS User Authorization File and return
  4264.  * a specific user's record.
  4265.  */
  4266.  
  4267. static struct UAF retuaf;
  4268.  
  4269. struct UAF *
  4270. get_uaf_name (uname)
  4271.      char * uname;
  4272. {
  4273.   register status;
  4274.   struct FAB uaf_fab;
  4275.   struct RAB uaf_rab;
  4276.   
  4277.   uaf_fab = cc$rms_fab;
  4278.   uaf_rab = cc$rms_rab;
  4279.   /* initialize fab fields */
  4280.   uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
  4281.   uaf_fab.fab$b_fns = 21;
  4282.   uaf_fab.fab$b_fac = FAB$M_GET;
  4283.   uaf_fab.fab$b_org = FAB$C_IDX;
  4284.   uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
  4285.   /* initialize rab fields */
  4286.   uaf_rab.rab$l_fab = &uaf_fab;
  4287.   /* open the User Authorization File */
  4288.   status = SYS$OPEN (&uaf_fab);
  4289.   if (!(status&1))
  4290.     {
  4291.       errno = EVMSERR;
  4292.       vaxc$errno = status;
  4293.       return 0;
  4294.     }
  4295.   status = SYS$CONNECT (&uaf_rab);
  4296.   if (!(status&1))
  4297.     {
  4298.       errno = EVMSERR;
  4299.       vaxc$errno = status;
  4300.       return 0;
  4301.     }
  4302.   /* read the requested record - index is in uname */
  4303.   uaf_rab.rab$l_kbf = uname;
  4304.   uaf_rab.rab$b_ksz = strlen (uname);
  4305.   uaf_rab.rab$b_rac = RAB$C_KEY;
  4306.   uaf_rab.rab$l_ubf = (char *)&retuaf;
  4307.   uaf_rab.rab$w_usz = sizeof retuaf;
  4308.   status = SYS$GET (&uaf_rab);
  4309.   if (!(status&1))
  4310.     {
  4311.       errno = EVMSERR;
  4312.       vaxc$errno = status;
  4313.       return 0;
  4314.     }
  4315.   /* close the User Authorization File */
  4316.   status = SYS$DISCONNECT (&uaf_rab);
  4317.   if (!(status&1))
  4318.     {
  4319.       errno = EVMSERR;
  4320.       vaxc$errno = status;
  4321.       return 0;
  4322.     }
  4323.   status = SYS$CLOSE (&uaf_fab);
  4324.   if (!(status&1))
  4325.     {
  4326.       errno = EVMSERR;
  4327.       vaxc$errno = status;
  4328.       return 0;
  4329.     }
  4330.   return &retuaf;
  4331. }
  4332.  
  4333. struct UAF *
  4334. get_uaf_uic (uic)
  4335.      unsigned long uic;
  4336. {
  4337.   register status;
  4338.   struct FAB uaf_fab;
  4339.   struct RAB uaf_rab;
  4340.   
  4341.   uaf_fab = cc$rms_fab;
  4342.   uaf_rab = cc$rms_rab;
  4343.   /* initialize fab fields */
  4344.   uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
  4345.   uaf_fab.fab$b_fns = 21;
  4346.   uaf_fab.fab$b_fac = FAB$M_GET;
  4347.   uaf_fab.fab$b_org = FAB$C_IDX;
  4348.   uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
  4349.   /* initialize rab fields */
  4350.   uaf_rab.rab$l_fab = &uaf_fab;
  4351.   /* open the User Authorization File */
  4352.   status = SYS$OPEN (&uaf_fab);
  4353.   if (!(status&1))
  4354.     {
  4355.       errno = EVMSERR;
  4356.       vaxc$errno = status;
  4357.       return 0;
  4358.     }
  4359.   status = SYS$CONNECT (&uaf_rab);
  4360.   if (!(status&1))
  4361.     {
  4362.       errno = EVMSERR;
  4363.       vaxc$errno = status;
  4364.       return 0;
  4365.     }
  4366.   /* read the requested record - index is in uic */
  4367.   uaf_rab.rab$b_krf = 1;    /* 1st alternate key */
  4368.   uaf_rab.rab$l_kbf = (char *) &uic;
  4369.   uaf_rab.rab$b_ksz = sizeof uic;
  4370.   uaf_rab.rab$b_rac = RAB$C_KEY;
  4371.   uaf_rab.rab$l_ubf = (char *)&retuaf;
  4372.   uaf_rab.rab$w_usz = sizeof retuaf;
  4373.   status = SYS$GET (&uaf_rab);
  4374.   if (!(status&1))
  4375.     {
  4376.       errno = EVMSERR;
  4377.       vaxc$errno = status;
  4378.       return 0;
  4379.     }
  4380.   /* close the User Authorization File */
  4381.   status = SYS$DISCONNECT (&uaf_rab);
  4382.   if (!(status&1))
  4383.     {
  4384.       errno = EVMSERR;
  4385.       vaxc$errno = status;
  4386.       return 0;
  4387.     }
  4388.   status = SYS$CLOSE (&uaf_fab);
  4389.   if (!(status&1))
  4390.     {
  4391.       errno = EVMSERR;
  4392.       vaxc$errno = status;
  4393.       return 0;
  4394.     }
  4395.   return &retuaf;
  4396. }
  4397.  
  4398. static struct passwd retpw;
  4399.  
  4400. struct passwd *
  4401. cnv_uaf_pw (up)
  4402.      struct UAF * up;
  4403. {
  4404.   char * ptr;
  4405.  
  4406.   /* copy these out first because if the username is 32 chars, the next
  4407.      section will overwrite the first byte of the UIC */
  4408.   retpw.pw_uid = up->uaf$w_mem;
  4409.   retpw.pw_gid = up->uaf$w_grp;
  4410.  
  4411.   /* I suppose this is not the best sytle, to possibly overwrite one
  4412.      byte beyond the end of the field, but what the heck... */
  4413.   ptr = &up->uaf$t_username[UAF$S_USERNAME];
  4414.   while (ptr[-1] == ' ')
  4415.     ptr--;
  4416.   *ptr = '\0';
  4417.   strcpy (retpw.pw_name, up->uaf$t_username);
  4418.  
  4419.   /* the rest of these are counted ascii strings */
  4420.   strncpy (retpw.pw_gecos, &up->uaf$t_owner[1], up->uaf$t_owner[0]);
  4421.   retpw.pw_gecos[up->uaf$t_owner[0]] = '\0';
  4422.   strncpy (retpw.pw_dir, &up->uaf$t_defdev[1], up->uaf$t_defdev[0]);
  4423.   retpw.pw_dir[up->uaf$t_defdev[0]] = '\0';
  4424.   strncat (retpw.pw_dir, &up->uaf$t_defdir[1], up->uaf$t_defdir[0]);
  4425.   retpw.pw_dir[up->uaf$t_defdev[0] + up->uaf$t_defdir[0]] = '\0';
  4426.   strncpy (retpw.pw_shell, &up->uaf$t_defcli[1], up->uaf$t_defcli[0]);
  4427.   retpw.pw_shell[up->uaf$t_defcli[0]] = '\0';
  4428.  
  4429.   return &retpw;
  4430. }
  4431. #else /* not READ_SYSUAF */
  4432. static struct passwd retpw;
  4433. #endif /* not READ_SYSUAF */
  4434.  
  4435. struct passwd *
  4436. getpwnam (name)
  4437.      char * name;
  4438. {
  4439. #ifdef READ_SYSUAF
  4440.   struct UAF *up;
  4441. #else
  4442.   char * user;
  4443.   char * dir;
  4444.   unsigned char * full;
  4445. #endif /* READ_SYSUAF */
  4446.   char *ptr = name;
  4447.  
  4448.   while (*ptr)
  4449.     {
  4450.       if ('a' <= *ptr && *ptr <= 'z')
  4451.     *ptr -= 040;
  4452.       ptr++;
  4453.     }
  4454. #ifdef READ_SYSUAF
  4455.   if (!(up = get_uaf_name (name)))
  4456.     return 0;
  4457.   return cnv_uaf_pw (up);
  4458. #else
  4459.   if (strcmp (name, getenv ("USER")) == 0)
  4460.     {
  4461.       retpw.pw_uid = getuid ();
  4462.       retpw.pw_gid = getgid ();
  4463.       strcpy (retpw.pw_name, name);
  4464.       if (full = egetenv ("FULLNAME"))
  4465.     strcpy (retpw.pw_gecos, full);
  4466.       else
  4467.     *retpw.pw_gecos = '\0';
  4468.       strcpy (retpw.pw_dir, egetenv ("HOME"));
  4469.       *retpw.pw_shell = '\0';
  4470.       return &retpw;
  4471.     }
  4472.   else
  4473.     return 0;
  4474. #endif /* not READ_SYSUAF */
  4475. }
  4476.  
  4477. struct passwd *
  4478. getpwuid (uid)
  4479.      unsigned long uid;
  4480. {
  4481. #ifdef READ_SYSUAF
  4482.   struct UAF * up;
  4483.  
  4484.   if (!(up = get_uaf_uic (uid)))
  4485.     return 0;
  4486.   return cnv_uaf_pw (up);
  4487. #else
  4488.   if (uid == sys_getuid ())
  4489.     return getpwnam (egetenv ("USER"));
  4490.   else
  4491.     return 0;
  4492. #endif /* not READ_SYSUAF */
  4493. }
  4494.  
  4495. /* return total address space available to the current process.  This is
  4496.    the sum of the current p0 size, p1 size and free page table entries
  4497.    available. */
  4498. vlimit ()
  4499. {
  4500.   int item_code;
  4501.   unsigned long free_pages;
  4502.   unsigned long frep0va;
  4503.   unsigned long frep1va;
  4504.   register status;
  4505.  
  4506.   item_code = JPI$_FREPTECNT;
  4507.   if (((status = LIB$GETJPI (&item_code, 0, 0, &free_pages)) & 1) == 0)
  4508.     {
  4509.       errno = EVMSERR;
  4510.       vaxc$errno = status;
  4511.       return -1;
  4512.     }
  4513.   free_pages *= 512;
  4514.  
  4515.   item_code = JPI$_FREP0VA;
  4516.   if (((status = LIB$GETJPI (&item_code, 0, 0, &frep0va)) & 1) == 0)
  4517.     {
  4518.       errno = EVMSERR;
  4519.       vaxc$errno = status;
  4520.       return -1;
  4521.     }
  4522.   item_code = JPI$_FREP1VA;
  4523.   if (((status = LIB$GETJPI (&item_code, 0, 0, &frep1va)) & 1) == 0)
  4524.     {
  4525.       errno = EVMSERR;
  4526.       vaxc$errno = status;
  4527.       return -1;
  4528.     }
  4529.  
  4530.   return free_pages + frep0va + (0x7fffffff - frep1va);
  4531. }
  4532.  
  4533. define_logical_name (varname, string)
  4534.      char *varname;
  4535.      char *string;
  4536. {
  4537.   struct dsc$descriptor_s strdsc =
  4538.     {strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string};
  4539.   struct dsc$descriptor_s envdsc =
  4540.     {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
  4541.   struct dsc$descriptor_s lnmdsc =
  4542.     {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
  4543.  
  4544.   return LIB$SET_LOGICAL (&envdsc, &strdsc, &lnmdsc, 0, 0);
  4545. }
  4546.  
  4547. delete_logical_name (varname)
  4548.      char *varname;
  4549. {
  4550.   struct dsc$descriptor_s envdsc =
  4551.     {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
  4552.   struct dsc$descriptor_s lnmdsc =
  4553.     {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
  4554.  
  4555.   return LIB$DELETE_LOGICAL (&envdsc, &lnmdsc);
  4556. }
  4557.  
  4558. ulimit ()
  4559. {}
  4560.  
  4561. setpgrp ()
  4562. {}
  4563.  
  4564. execvp ()
  4565. {
  4566.   error ("execvp system call not implemented");
  4567. }
  4568.  
  4569. int
  4570. rename (from, to)
  4571.      char *from, *to;
  4572. {
  4573.   int status;
  4574.   struct FAB from_fab = cc$rms_fab, to_fab = cc$rms_fab;
  4575.   struct NAM from_nam = cc$rms_nam, to_nam = cc$rms_nam;
  4576.   char from_esn[NAM$C_MAXRSS];
  4577.   char to_esn[NAM$C_MAXRSS];
  4578.  
  4579.   from_fab.fab$l_fna = from;
  4580.   from_fab.fab$b_fns = strlen (from);
  4581.   from_fab.fab$l_nam = &from_nam;
  4582.   from_fab.fab$l_fop = FAB$M_NAM;
  4583.  
  4584.   from_nam.nam$l_esa = from_esn;
  4585.   from_nam.nam$b_ess = sizeof from_esn;
  4586.  
  4587.   to_fab.fab$l_fna = to;
  4588.   to_fab.fab$b_fns = strlen (to);
  4589.   to_fab.fab$l_nam = &to_nam;
  4590.   to_fab.fab$l_fop = FAB$M_NAM;
  4591.  
  4592.   to_nam.nam$l_esa = to_esn;
  4593.   to_nam.nam$b_ess = sizeof to_esn;
  4594.  
  4595.   status = SYS$RENAME (&from_fab, 0, 0, &to_fab);
  4596.  
  4597.   if (status & 1)
  4598.     return 0;
  4599.   else
  4600.     {
  4601.       if (status == RMS$_DEV)
  4602.     errno = EXDEV;
  4603.       else
  4604.     errno = EVMSERR;
  4605.       vaxc$errno = status;
  4606.       return -1;
  4607.     }
  4608. }
  4609.  
  4610. /* This function renames a file like `rename', but it strips
  4611.    the version number from the "to" filename, such that the "to" file is
  4612.    will always be a new version.  It also sets the file protection once it is
  4613.    finished.  The protection that we will use is stored in fab_final_pro,
  4614.    and was set when we did a creat_copy_attrs to create the file that we
  4615.    are renaming.
  4616.  
  4617.    We could use the chmod function, but Eunichs uses 3 bits per user category
  4618.    to describe the protection, and VMS uses 4 (write and delete are separate
  4619.    bits).  To maintain portability, the VMS implementation of `chmod' wires
  4620.    the W and D bits together.  */
  4621.  
  4622.  
  4623. static struct fibdef fib;    /* We need this initialized to zero */
  4624. char vms_file_written[NAM$C_MAXRSS];
  4625.  
  4626. int
  4627. rename_sans_version (from,to)
  4628.      char *from, *to;
  4629. {
  4630.   short int chan;
  4631.   int stat;
  4632.   short int iosb[4];
  4633.   int status;
  4634.   struct FAB to_fab = cc$rms_fab;
  4635.   struct NAM to_nam = cc$rms_nam;
  4636.   struct dsc$descriptor fib_d ={sizeof (fib),0,0,(char*) &fib};
  4637.   struct dsc$descriptor fib_attr[2]
  4638.     = {{sizeof (fab_final_pro),ATR$C_FPRO,0,(char*) &fab_final_pro},{0,0,0,0}};
  4639.   char to_esn[NAM$C_MAXRSS];
  4640.  
  4641.   $DESCRIPTOR (disk,to_esn);
  4642.  
  4643.   to_fab.fab$l_fna = to;
  4644.   to_fab.fab$b_fns = strlen (to);
  4645.   to_fab.fab$l_nam = &to_nam;
  4646.   to_fab.fab$l_fop = FAB$M_NAM;
  4647.  
  4648.   to_nam.nam$l_esa = to_esn;
  4649.   to_nam.nam$b_ess = sizeof to_esn;
  4650.  
  4651.   status = SYS$PARSE (&to_fab, 0, 0); /* figure out the full file name */
  4652.  
  4653.   if (to_nam.nam$l_fnb && NAM$M_EXP_VER)
  4654.     *(to_nam.nam$l_ver) = '\0';
  4655.  
  4656.   stat = rename (from, to_esn);
  4657.   if (stat < 0)
  4658.     return stat;
  4659.  
  4660.   strcpy (vms_file_written, to_esn);
  4661.  
  4662.   to_fab.fab$l_fna = vms_file_written; /* this points to the versionless name */
  4663.   to_fab.fab$b_fns = strlen (vms_file_written);
  4664.  
  4665.   /* Now set the file protection to the correct value */
  4666.   SYS$OPEN (&to_fab, 0, 0);    /* This fills in the nam$w_fid fields */
  4667.  
  4668.   /* Copy these fields into the fib */
  4669.   fib.fib$r_fid_overlay.fib$w_fid[0] = to_nam.nam$w_fid[0];
  4670.   fib.fib$r_fid_overlay.fib$w_fid[1] = to_nam.nam$w_fid[1];
  4671.   fib.fib$r_fid_overlay.fib$w_fid[2] = to_nam.nam$w_fid[2];
  4672.  
  4673.   SYS$CLOSE (&to_fab, 0, 0);
  4674.  
  4675.   stat = SYS$ASSIGN (&disk, &chan, 0, 0); /* open a channel to the disk */
  4676.   if (!stat)
  4677.     LIB$SIGNAL (stat);
  4678.   stat = SYS$QIOW (0, chan, IO$_MODIFY, iosb, 0, 0, &fib_d,
  4679.            0, 0, 0, &fib_attr, 0);
  4680.   if (!stat)
  4681.     LIB$SIGNAL (stat);
  4682.   stat = SYS$DASSGN (chan);
  4683.   if (!stat)
  4684.     LIB$SIGNAL (stat);
  4685.   strcpy (vms_file_written, to_esn); /* We will write this to the terminal*/
  4686.   return 0;
  4687. }
  4688.  
  4689. link (file, new)
  4690.      char * file, * new;
  4691. {
  4692.   register status;
  4693.   struct FAB fab;
  4694.   struct NAM nam;
  4695.   unsigned short fid[3];
  4696.   char esa[NAM$C_MAXRSS];
  4697.  
  4698.   fab = cc$rms_fab;
  4699.   fab.fab$l_fop = FAB$M_OFP;
  4700.   fab.fab$l_fna = file;
  4701.   fab.fab$b_fns = strlen (file);
  4702.   fab.fab$l_nam = &nam;
  4703.  
  4704.   nam = cc$rms_nam;
  4705.   nam.nam$l_esa = esa;
  4706.   nam.nam$b_ess = NAM$C_MAXRSS;
  4707.  
  4708.   status = SYS$PARSE (&fab);
  4709.   if ((status & 1) == 0)
  4710.     {
  4711.       errno = EVMSERR;
  4712.       vaxc$errno = status;
  4713.       return -1;
  4714.     }
  4715.   status = SYS$SEARCH (&fab);
  4716.   if ((status & 1) == 0)
  4717.     {
  4718.       errno = EVMSERR;
  4719.       vaxc$errno = status;
  4720.       return -1;
  4721.     }
  4722.  
  4723.   fid[0] = nam.nam$w_fid[0];
  4724.   fid[1] = nam.nam$w_fid[1];
  4725.   fid[2] = nam.nam$w_fid[2];
  4726.  
  4727.   fab.fab$l_fna = new;
  4728.   fab.fab$b_fns = strlen (new);
  4729.  
  4730.   status = SYS$PARSE (&fab);
  4731.   if ((status & 1) == 0)
  4732.     {
  4733.       errno = EVMSERR;
  4734.       vaxc$errno = status;
  4735.       return -1;
  4736.     }
  4737.  
  4738.   nam.nam$w_fid[0] = fid[0];
  4739.   nam.nam$w_fid[1] = fid[1];
  4740.   nam.nam$w_fid[2] = fid[2];
  4741.  
  4742.   nam.nam$l_esa = nam.nam$l_name;
  4743.   nam.nam$b_esl = nam.nam$b_name + nam.nam$b_type + nam.nam$b_ver;
  4744.  
  4745.   status = SYS$ENTER (&fab);
  4746.   if ((status & 1) == 0)
  4747.     {
  4748.       errno = EVMSERR;
  4749.       vaxc$errno = status;
  4750.       return -1;
  4751.     }
  4752.  
  4753.   return 0;
  4754. }
  4755.  
  4756. _VOID_
  4757. croak (badfunc)
  4758.      char *badfunc;
  4759. {
  4760.   printf ("%s not yet implemented\r\n", badfunc);
  4761.   reset_sys_modes ();
  4762.   exit (1);
  4763. }
  4764.  
  4765. long
  4766. random ()
  4767. {
  4768.   /* Arrange to return a range centered on zero.  */
  4769.   return rand () - (1 << 30);
  4770. }
  4771.  
  4772. _VOID_
  4773. srandom (seed)
  4774. {
  4775.   srand (seed);
  4776. }
  4777. #endif /* VMS */
  4778.  
  4779. #ifdef AIX
  4780.  
  4781. /* Called from init_sys_modes.  */
  4782. hft_init ()
  4783. {
  4784.   int junk;
  4785.  
  4786.   /* If we're not on an HFT we shouldn't do any of this.  We determine
  4787.      if we are on an HFT by trying to get an HFT error code.  If this
  4788.      call fails, we're not on an HFT. */ 
  4789. #ifdef IBMR2AIX
  4790.   if (ioctl (0, HFQERROR, &junk) < 0)
  4791.     return;
  4792. #else /* not IBMR2AIX */
  4793.   if (ioctl (0, HFQEIO, 0) < 0)
  4794.     return;
  4795. #endif /* not IBMR2AIX */
  4796.  
  4797.   /* On AIX the default hft keyboard mapping uses backspace rather than delete
  4798.      as the rubout key's ASCII code.  Here this is changed.  The bug is that
  4799.      there's no way to determine the old mapping, so in reset_sys_modes
  4800.      we need to assume that the normal map had been present.  Of course, this
  4801.      code also doesn't help if on a terminal emulator which doesn't understand
  4802.      HFT VTD's. */
  4803.   {
  4804.     struct hfbuf buf;
  4805.     struct hfkeymap keymap;
  4806.  
  4807.     buf.hf_bufp = (char *)&keymap;
  4808.     buf.hf_buflen = sizeof (keymap);
  4809.     keymap.hf_nkeys = 2;
  4810.     keymap.hfkey[0].hf_kpos = 15;
  4811.     keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
  4812. #ifdef IBMR2AIX
  4813.     keymap.hfkey[0].hf_keyidh = '<';
  4814. #else /* not IBMR2AIX */
  4815.     keymap.hfkey[0].hf_page = '<';
  4816. #endif /* not IBMR2AIX */
  4817.     keymap.hfkey[0].hf_char = 127;
  4818.     keymap.hfkey[1].hf_kpos = 15;
  4819.     keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
  4820. #ifdef IBMR2AIX
  4821.     keymap.hfkey[1].hf_keyidh = '<';
  4822. #else /* not IBMR2AIX */
  4823.     keymap.hfkey[1].hf_page = '<';
  4824. #endif /* not IBMR2AIX */
  4825.     keymap.hfkey[1].hf_char = 127;
  4826.     hftctl (0, HFSKBD, &buf);
  4827.   }
  4828.   /* The HFT system on AIX doesn't optimize for scrolling, so it's really ugly
  4829.      at times.  */
  4830.   line_ins_del_ok = char_ins_del_ok = 0;
  4831. }
  4832.  
  4833. /* Reset the rubout key to backspace. */
  4834.  
  4835. hft_reset ()
  4836. {
  4837.   struct hfbuf buf;
  4838.   struct hfkeymap keymap;
  4839.   int junk;
  4840.  
  4841. #ifdef IBMR2AIX
  4842.   if (ioctl (0, HFQERROR, &junk) < 0)
  4843.     return;
  4844. #else /* not IBMR2AIX */
  4845.   if (ioctl (0, HFQEIO, 0) < 0)
  4846.     return;
  4847. #endif /* not IBMR2AIX */
  4848.  
  4849.   buf.hf_bufp = (char *)&keymap;
  4850.   buf.hf_buflen = sizeof (keymap);
  4851.   keymap.hf_nkeys = 2;
  4852.   keymap.hfkey[0].hf_kpos = 15;
  4853.   keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
  4854. #ifdef IBMR2AIX
  4855.   keymap.hfkey[0].hf_keyidh = '<';
  4856. #else /* not IBMR2AIX */
  4857.   keymap.hfkey[0].hf_page = '<';
  4858. #endif /* not IBMR2AIX */
  4859.   keymap.hfkey[0].hf_char = 8;
  4860.   keymap.hfkey[1].hf_kpos = 15;
  4861.   keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
  4862. #ifdef IBMR2AIX
  4863.   keymap.hfkey[1].hf_keyidh = '<';
  4864. #else /* not IBMR2AIX */
  4865.   keymap.hfkey[1].hf_page = '<';
  4866. #endif /* not IBMR2AIX */
  4867.   keymap.hfkey[1].hf_char = 8;
  4868.   hftctl (0, HFSKBD, &buf);
  4869. }
  4870.  
  4871. #endif /* AIX */
  4872.